rustc_mir_dataflow/impls/
liveness.rsuse rustc_index::bit_set::BitSet;
use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
use rustc_middle::mir::{
self, CallReturnPlaces, Local, Location, Place, StatementKind, TerminatorEdges,
};
use crate::{Analysis, Backward, GenKill};
pub struct MaybeLiveLocals;
impl<'tcx> Analysis<'tcx> for MaybeLiveLocals {
type Domain = BitSet<Local>;
type Direction = Backward;
const NAME: &'static str = "liveness";
fn bottom_value(&self, body: &mir::Body<'tcx>) -> Self::Domain {
BitSet::new_empty(body.local_decls.len())
}
fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut Self::Domain) {
}
fn apply_primary_statement_effect(
&mut self,
state: &mut Self::Domain,
statement: &mir::Statement<'tcx>,
location: Location,
) {
TransferFunction(state).visit_statement(statement, location);
}
fn apply_primary_terminator_effect<'mir>(
&mut self,
state: &mut Self::Domain,
terminator: &'mir mir::Terminator<'tcx>,
location: Location,
) -> TerminatorEdges<'mir, 'tcx> {
TransferFunction(state).visit_terminator(terminator, location);
terminator.edges()
}
fn apply_call_return_effect(
&mut self,
state: &mut Self::Domain,
_block: mir::BasicBlock,
return_places: CallReturnPlaces<'_, 'tcx>,
) {
if let CallReturnPlaces::Yield(resume_place) = return_places {
YieldResumeEffect(state).visit_place(
&resume_place,
PlaceContext::MutatingUse(MutatingUseContext::Yield),
Location::START,
)
} else {
return_places.for_each(|place| {
if let Some(local) = place.as_local() {
state.kill(local);
}
});
}
}
}
pub struct TransferFunction<'a>(pub &'a mut BitSet<Local>);
impl<'tcx> Visitor<'tcx> for TransferFunction<'_> {
fn visit_place(&mut self, place: &mir::Place<'tcx>, context: PlaceContext, location: Location) {
if let PlaceContext::MutatingUse(MutatingUseContext::Yield) = context {
return;
}
match DefUse::for_place(*place, context) {
Some(DefUse::Def) => {
if let PlaceContext::MutatingUse(
MutatingUseContext::Call | MutatingUseContext::AsmOutput,
) = context
{
} else {
self.0.kill(place.local);
}
}
Some(DefUse::Use) => self.0.gen_(place.local),
None => {}
}
self.visit_projection(place.as_ref(), context, location);
}
fn visit_local(&mut self, local: Local, context: PlaceContext, _: Location) {
DefUse::apply(self.0, local.into(), context);
}
}
struct YieldResumeEffect<'a>(&'a mut BitSet<Local>);
impl<'tcx> Visitor<'tcx> for YieldResumeEffect<'_> {
fn visit_place(&mut self, place: &mir::Place<'tcx>, context: PlaceContext, location: Location) {
DefUse::apply(self.0, *place, context);
self.visit_projection(place.as_ref(), context, location);
}
fn visit_local(&mut self, local: Local, context: PlaceContext, _: Location) {
DefUse::apply(self.0, local.into(), context);
}
}
#[derive(Eq, PartialEq, Clone)]
enum DefUse {
Def,
Use,
}
impl DefUse {
fn apply(state: &mut BitSet<Local>, place: Place<'_>, context: PlaceContext) {
match DefUse::for_place(place, context) {
Some(DefUse::Def) => state.kill(place.local),
Some(DefUse::Use) => state.gen_(place.local),
None => {}
}
}
fn for_place(place: Place<'_>, context: PlaceContext) -> Option<DefUse> {
match context {
PlaceContext::NonUse(_) => None,
PlaceContext::MutatingUse(
MutatingUseContext::Call
| MutatingUseContext::Yield
| MutatingUseContext::AsmOutput
| MutatingUseContext::Store
| MutatingUseContext::Deinit,
) => {
if place.is_indirect() {
Some(DefUse::Use)
} else if place.projection.is_empty() {
Some(DefUse::Def)
} else {
None
}
}
PlaceContext::MutatingUse(MutatingUseContext::SetDiscriminant) => {
place.is_indirect().then_some(DefUse::Use)
}
PlaceContext::MutatingUse(
MutatingUseContext::RawBorrow
| MutatingUseContext::Borrow
| MutatingUseContext::Drop
| MutatingUseContext::Retag,
)
| PlaceContext::NonMutatingUse(
NonMutatingUseContext::RawBorrow
| NonMutatingUseContext::Copy
| NonMutatingUseContext::Inspect
| NonMutatingUseContext::Move
| NonMutatingUseContext::PlaceMention
| NonMutatingUseContext::FakeBorrow
| NonMutatingUseContext::SharedBorrow,
) => Some(DefUse::Use),
PlaceContext::MutatingUse(MutatingUseContext::Projection)
| PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection) => {
unreachable!("A projection could be a def or a use and must be handled separately")
}
}
}
}
pub struct MaybeTransitiveLiveLocals<'a> {
always_live: &'a BitSet<Local>,
}
impl<'a> MaybeTransitiveLiveLocals<'a> {
pub fn new(always_live: &'a BitSet<Local>) -> Self {
MaybeTransitiveLiveLocals { always_live }
}
}
impl<'a, 'tcx> Analysis<'tcx> for MaybeTransitiveLiveLocals<'a> {
type Domain = BitSet<Local>;
type Direction = Backward;
const NAME: &'static str = "transitive liveness";
fn bottom_value(&self, body: &mir::Body<'tcx>) -> Self::Domain {
BitSet::new_empty(body.local_decls.len())
}
fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut Self::Domain) {
}
fn apply_primary_statement_effect(
&mut self,
state: &mut Self::Domain,
statement: &mir::Statement<'tcx>,
location: Location,
) {
let destination = match &statement.kind {
StatementKind::Assign(assign) => assign.1.is_safe_to_remove().then_some(assign.0),
StatementKind::SetDiscriminant { place, .. } | StatementKind::Deinit(place) => {
Some(**place)
}
StatementKind::FakeRead(_)
| StatementKind::StorageLive(_)
| StatementKind::StorageDead(_)
| StatementKind::Retag(..)
| StatementKind::AscribeUserType(..)
| StatementKind::PlaceMention(..)
| StatementKind::Coverage(..)
| StatementKind::Intrinsic(..)
| StatementKind::ConstEvalCounter
| StatementKind::BackwardIncompatibleDropHint { .. }
| StatementKind::Nop => None,
};
if let Some(destination) = destination {
if !destination.is_indirect()
&& !state.contains(destination.local)
&& !self.always_live.contains(destination.local)
{
return;
}
}
TransferFunction(state).visit_statement(statement, location);
}
fn apply_primary_terminator_effect<'mir>(
&mut self,
state: &mut Self::Domain,
terminator: &'mir mir::Terminator<'tcx>,
location: Location,
) -> TerminatorEdges<'mir, 'tcx> {
TransferFunction(state).visit_terminator(terminator, location);
terminator.edges()
}
fn apply_call_return_effect(
&mut self,
state: &mut Self::Domain,
_block: mir::BasicBlock,
return_places: CallReturnPlaces<'_, 'tcx>,
) {
if let CallReturnPlaces::Yield(resume_place) = return_places {
YieldResumeEffect(state).visit_place(
&resume_place,
PlaceContext::MutatingUse(MutatingUseContext::Yield),
Location::START,
)
} else {
return_places.for_each(|place| {
if let Some(local) = place.as_local() {
state.remove(local);
}
});
}
}
}