Skip to content

Visit only statements in always live locals #81440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 12 additions & 19 deletions compiler/rustc_mir/src/util/storage.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use rustc_index::bit_set::BitSet;
use rustc_middle::mir::visit::Visitor;
use rustc_middle::mir::{self, Local, Location};
use rustc_middle::mir::{self, Local};

/// The set of locals in a MIR body that do not have `StorageLive`/`StorageDead` annotations.
///
Expand All @@ -13,12 +12,18 @@ pub struct AlwaysLiveLocals(BitSet<Local>);

impl AlwaysLiveLocals {
pub fn new(body: &mir::Body<'tcx>) -> Self {
let mut ret = AlwaysLiveLocals(BitSet::new_filled(body.local_decls.len()));

let mut vis = StorageAnnotationVisitor(&mut ret);
vis.visit_body(body);
let mut always_live_locals = AlwaysLiveLocals(BitSet::new_filled(body.local_decls.len()));

for block in body.basic_blocks() {
for statement in &block.statements {
use mir::StatementKind::{StorageDead, StorageLive};
if let StorageLive(l) | StorageDead(l) = statement.kind {
always_live_locals.0.remove(l);
}
}
}

ret
always_live_locals
}

pub fn into_inner(self) -> BitSet<Local> {
Expand All @@ -33,15 +38,3 @@ impl std::ops::Deref for AlwaysLiveLocals {
&self.0
}
}

/// Removes locals that have `Storage*` annotations from `AlwaysLiveLocals`.
struct StorageAnnotationVisitor<'a>(&'a mut AlwaysLiveLocals);

impl Visitor<'tcx> for StorageAnnotationVisitor<'_> {
fn visit_statement(&mut self, statement: &mir::Statement<'tcx>, _location: Location) {
use mir::StatementKind::{StorageDead, StorageLive};
if let StorageLive(l) | StorageDead(l) = statement.kind {
(self.0).0.remove(l);
}
}
}