rustc_mir_dataflow/impls/
storage_liveness.rsuse std::borrow::Cow;
use rustc_index::bit_set::BitSet;
use rustc_middle::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor};
use rustc_middle::mir::*;
use super::MaybeBorrowedLocals;
use crate::{Analysis, GenKill, ResultsCursor};
pub fn always_storage_live_locals(body: &Body<'_>) -> BitSet<Local> {
let mut always_live_locals = BitSet::new_filled(body.local_decls.len());
for block in &*body.basic_blocks {
for statement in &block.statements {
if let StatementKind::StorageLive(l) | StatementKind::StorageDead(l) = statement.kind {
always_live_locals.remove(l);
}
}
}
always_live_locals
}
pub struct MaybeStorageLive<'a> {
always_live_locals: Cow<'a, BitSet<Local>>,
}
impl<'a> MaybeStorageLive<'a> {
pub fn new(always_live_locals: Cow<'a, BitSet<Local>>) -> Self {
MaybeStorageLive { always_live_locals }
}
}
impl<'a, 'tcx> Analysis<'tcx> for MaybeStorageLive<'a> {
type Domain = BitSet<Local>;
const NAME: &'static str = "maybe_storage_live";
fn bottom_value(&self, body: &Body<'tcx>) -> Self::Domain {
BitSet::new_empty(body.local_decls.len())
}
fn initialize_start_block(&self, body: &Body<'tcx>, state: &mut Self::Domain) {
state.union(&*self.always_live_locals);
for arg in body.args_iter() {
state.insert(arg);
}
}
fn apply_primary_statement_effect(
&mut self,
state: &mut Self::Domain,
stmt: &Statement<'tcx>,
_: Location,
) {
match stmt.kind {
StatementKind::StorageLive(l) => state.gen_(l),
StatementKind::StorageDead(l) => state.kill(l),
_ => (),
}
}
}
pub struct MaybeStorageDead<'a> {
always_live_locals: Cow<'a, BitSet<Local>>,
}
impl<'a> MaybeStorageDead<'a> {
pub fn new(always_live_locals: Cow<'a, BitSet<Local>>) -> Self {
MaybeStorageDead { always_live_locals }
}
}
impl<'a, 'tcx> Analysis<'tcx> for MaybeStorageDead<'a> {
type Domain = BitSet<Local>;
const NAME: &'static str = "maybe_storage_dead";
fn bottom_value(&self, body: &Body<'tcx>) -> Self::Domain {
BitSet::new_empty(body.local_decls.len())
}
fn initialize_start_block(&self, body: &Body<'tcx>, state: &mut Self::Domain) {
assert_eq!(body.local_decls.len(), self.always_live_locals.domain_size());
for local in body.vars_and_temps_iter() {
if !self.always_live_locals.contains(local) {
state.insert(local);
}
}
}
fn apply_primary_statement_effect(
&mut self,
state: &mut Self::Domain,
stmt: &Statement<'tcx>,
_: Location,
) {
match stmt.kind {
StatementKind::StorageLive(l) => state.kill(l),
StatementKind::StorageDead(l) => state.gen_(l),
_ => (),
}
}
}
type BorrowedLocalsResults<'mir, 'tcx> = ResultsCursor<'mir, 'tcx, MaybeBorrowedLocals>;
pub struct MaybeRequiresStorage<'mir, 'tcx> {
borrowed_locals: BorrowedLocalsResults<'mir, 'tcx>,
}
impl<'mir, 'tcx> MaybeRequiresStorage<'mir, 'tcx> {
pub fn new(borrowed_locals: BorrowedLocalsResults<'mir, 'tcx>) -> Self {
MaybeRequiresStorage { borrowed_locals }
}
}
impl<'tcx> Analysis<'tcx> for MaybeRequiresStorage<'_, 'tcx> {
type Domain = BitSet<Local>;
const NAME: &'static str = "requires_storage";
fn bottom_value(&self, body: &Body<'tcx>) -> Self::Domain {
BitSet::new_empty(body.local_decls.len())
}
fn initialize_start_block(&self, body: &Body<'tcx>, state: &mut Self::Domain) {
for arg in body.args_iter().skip(1) {
state.insert(arg);
}
}
fn apply_early_statement_effect(
&mut self,
state: &mut Self::Domain,
stmt: &Statement<'tcx>,
loc: Location,
) {
MaybeBorrowedLocals::transfer_function(state).visit_statement(stmt, loc);
match &stmt.kind {
StatementKind::StorageDead(l) => state.kill(*l),
StatementKind::Assign(box (place, _))
| StatementKind::SetDiscriminant { box place, .. }
| StatementKind::Deinit(box place) => {
state.gen_(place.local);
}
StatementKind::AscribeUserType(..)
| StatementKind::PlaceMention(..)
| StatementKind::Coverage(..)
| StatementKind::FakeRead(..)
| StatementKind::ConstEvalCounter
| StatementKind::Nop
| StatementKind::Retag(..)
| StatementKind::Intrinsic(..)
| StatementKind::BackwardIncompatibleDropHint { .. }
| StatementKind::StorageLive(..) => {}
}
}
fn apply_primary_statement_effect(
&mut self,
state: &mut Self::Domain,
_: &Statement<'tcx>,
loc: Location,
) {
self.check_for_move(state, loc);
}
fn apply_early_terminator_effect(
&mut self,
state: &mut Self::Domain,
terminator: &Terminator<'tcx>,
loc: Location,
) {
MaybeBorrowedLocals::transfer_function(state).visit_terminator(terminator, loc);
match &terminator.kind {
TerminatorKind::Call { destination, .. } => {
state.gen_(destination.local);
}
TerminatorKind::Yield { .. } => {}
TerminatorKind::InlineAsm { operands, .. } => {
for op in operands {
match op {
InlineAsmOperand::Out { place, .. }
| InlineAsmOperand::InOut { out_place: place, .. } => {
if let Some(place) = place {
state.gen_(place.local);
}
}
InlineAsmOperand::In { .. }
| InlineAsmOperand::Const { .. }
| InlineAsmOperand::SymFn { .. }
| InlineAsmOperand::SymStatic { .. }
| InlineAsmOperand::Label { .. } => {}
}
}
}
TerminatorKind::UnwindTerminate(_)
| TerminatorKind::Assert { .. }
| TerminatorKind::Drop { .. }
| TerminatorKind::FalseEdge { .. }
| TerminatorKind::FalseUnwind { .. }
| TerminatorKind::CoroutineDrop
| TerminatorKind::Goto { .. }
| TerminatorKind::UnwindResume
| TerminatorKind::Return
| TerminatorKind::TailCall { .. }
| TerminatorKind::SwitchInt { .. }
| TerminatorKind::Unreachable => {}
}
}
fn apply_primary_terminator_effect<'t>(
&mut self,
state: &mut Self::Domain,
terminator: &'t Terminator<'tcx>,
loc: Location,
) -> TerminatorEdges<'t, 'tcx> {
match terminator.kind {
TerminatorKind::Call { destination, .. } => {
state.kill(destination.local);
}
TerminatorKind::InlineAsm { ref operands, .. } => {
CallReturnPlaces::InlineAsm(operands).for_each(|place| state.kill(place.local));
}
TerminatorKind::Yield { .. }
| TerminatorKind::UnwindTerminate(_)
| TerminatorKind::Assert { .. }
| TerminatorKind::Drop { .. }
| TerminatorKind::FalseEdge { .. }
| TerminatorKind::FalseUnwind { .. }
| TerminatorKind::CoroutineDrop
| TerminatorKind::Goto { .. }
| TerminatorKind::UnwindResume
| TerminatorKind::Return
| TerminatorKind::TailCall { .. }
| TerminatorKind::SwitchInt { .. }
| TerminatorKind::Unreachable => {}
}
self.check_for_move(state, loc);
terminator.edges()
}
fn apply_call_return_effect(
&mut self,
state: &mut Self::Domain,
_block: BasicBlock,
return_places: CallReturnPlaces<'_, 'tcx>,
) {
return_places.for_each(|place| state.gen_(place.local));
}
}
impl<'tcx> MaybeRequiresStorage<'_, 'tcx> {
fn check_for_move(&mut self, state: &mut <Self as Analysis<'tcx>>::Domain, loc: Location) {
let body = self.borrowed_locals.body();
let mut visitor = MoveVisitor { state, borrowed_locals: &mut self.borrowed_locals };
visitor.visit_location(body, loc);
}
}
struct MoveVisitor<'a, 'mir, 'tcx> {
borrowed_locals: &'a mut BorrowedLocalsResults<'mir, 'tcx>,
state: &'a mut BitSet<Local>,
}
impl<'tcx> Visitor<'tcx> for MoveVisitor<'_, '_, 'tcx> {
fn visit_local(&mut self, local: Local, context: PlaceContext, loc: Location) {
if PlaceContext::NonMutatingUse(NonMutatingUseContext::Move) == context {
self.borrowed_locals.seek_before_primary_effect(loc);
if !self.borrowed_locals.get().contains(local) {
self.state.kill(local);
}
}
}
}