2020-03-25 16:19:14 -07:00
|
|
|
use rustc_index::bit_set::BitSet;
|
|
|
|
use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
|
|
|
|
use rustc_middle::mir::{self, Local, Location};
|
|
|
|
|
2021-08-30 01:23:33 +01:00
|
|
|
use crate::{AnalysisDomain, Backward, CallReturnPlaces, GenKill, GenKillAnalysis};
|
2020-03-25 16:19:14 -07:00
|
|
|
|
|
|
|
/// A [live-variable dataflow analysis][liveness].
|
|
|
|
///
|
2020-05-07 13:13:02 -07:00
|
|
|
/// This analysis considers references as being used only at the point of the
|
|
|
|
/// borrow. In other words, this analysis does not track uses because of references that already
|
2020-10-28 19:31:16 +01:00
|
|
|
/// exist. See [this `mir-dataflow` test][flow-test] for an example. You almost never want to use
|
2020-05-07 13:13:02 -07:00
|
|
|
/// this analysis without also looking at the results of [`MaybeBorrowedLocals`].
|
|
|
|
///
|
2021-10-04 14:09:19 -07:00
|
|
|
/// ## Field-(in)sensitivity
|
|
|
|
///
|
|
|
|
/// As the name suggests, this analysis is field insensitive. If a projection of a variable `x` is
|
|
|
|
/// assigned to (e.g. `x.0 = 42`), it does not "define" `x` as far as liveness is concerned. In fact,
|
|
|
|
/// such an assignment is currently marked as a "use" of `x` in an attempt to be maximally
|
|
|
|
/// conservative.
|
|
|
|
///
|
|
|
|
/// ## Enums and `SetDiscriminant`
|
|
|
|
///
|
|
|
|
/// Assigning a literal value to an `enum` (e.g. `Option<i32>`), does not result in a simple
|
|
|
|
/// assignment of the form `_1 = /*...*/` in the MIR. For example, the following assignment to `x`:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// x = Some(4);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// compiles to this MIR
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// ((_1 as Some).0: i32) = const 4_i32;
|
|
|
|
/// discriminant(_1) = 1;
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// However, `MaybeLiveLocals` **does** mark `x` (`_1`) as "killed" after a statement like this.
|
|
|
|
/// That's because it treats the `SetDiscriminant` operation as a definition of `x`, even though
|
|
|
|
/// the writes that actually initialized the locals happened earlier.
|
|
|
|
///
|
|
|
|
/// This makes `MaybeLiveLocals` unsuitable for certain classes of optimization normally associated
|
|
|
|
/// with a live variables analysis, notably dead-store elimination. It's a dirty hack, but it works
|
2022-03-30 15:14:15 -04:00
|
|
|
/// okay for the generator state transform (currently the main consumer of this analysis).
|
2021-10-04 14:09:19 -07:00
|
|
|
///
|
2020-12-01 17:41:09 -05:00
|
|
|
/// [`MaybeBorrowedLocals`]: super::MaybeBorrowedLocals
|
2020-05-07 13:13:02 -07:00
|
|
|
/// [flow-test]: https://github.com/rust-lang/rust/blob/a08c47310c7d49cbdc5d7afb38408ba519967ecd/src/test/ui/mir-dataflow/liveness-ptr.rs
|
2020-03-25 16:19:14 -07:00
|
|
|
/// [liveness]: https://en.wikipedia.org/wiki/Live_variable_analysis
|
|
|
|
pub struct MaybeLiveLocals;
|
|
|
|
|
|
|
|
impl MaybeLiveLocals {
|
2021-12-14 12:02:45 -05:00
|
|
|
fn transfer_function<'a, T>(&self, trans: &'a mut T) -> TransferFunction<'a, T> {
|
2020-03-25 16:19:14 -07:00
|
|
|
TransferFunction(trans)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 12:02:45 -05:00
|
|
|
impl<'tcx> AnalysisDomain<'tcx> for MaybeLiveLocals {
|
2020-08-28 13:26:25 -07:00
|
|
|
type Domain = BitSet<Local>;
|
2020-03-25 16:19:14 -07:00
|
|
|
type Direction = Backward;
|
|
|
|
|
|
|
|
const NAME: &'static str = "liveness";
|
|
|
|
|
2020-08-28 13:26:25 -07:00
|
|
|
fn bottom_value(&self, body: &mir::Body<'tcx>) -> Self::Domain {
|
|
|
|
// bottom = not live
|
|
|
|
BitSet::new_empty(body.local_decls.len())
|
2020-03-25 16:19:14 -07:00
|
|
|
}
|
|
|
|
|
2020-08-28 13:26:25 -07:00
|
|
|
fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut Self::Domain) {
|
2020-03-25 16:19:14 -07:00
|
|
|
// No variables are live until we observe a use
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 12:02:45 -05:00
|
|
|
impl<'tcx> GenKillAnalysis<'tcx> for MaybeLiveLocals {
|
2020-08-28 13:26:25 -07:00
|
|
|
type Idx = Local;
|
|
|
|
|
2020-03-25 16:19:14 -07:00
|
|
|
fn statement_effect(
|
|
|
|
&self,
|
|
|
|
trans: &mut impl GenKill<Self::Idx>,
|
|
|
|
statement: &mir::Statement<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) {
|
|
|
|
self.transfer_function(trans).visit_statement(statement, location);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn terminator_effect(
|
|
|
|
&self,
|
|
|
|
trans: &mut impl GenKill<Self::Idx>,
|
|
|
|
terminator: &mir::Terminator<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) {
|
|
|
|
self.transfer_function(trans).visit_terminator(terminator, location);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call_return_effect(
|
|
|
|
&self,
|
|
|
|
trans: &mut impl GenKill<Self::Idx>,
|
|
|
|
_block: mir::BasicBlock,
|
2021-08-30 01:23:33 +01:00
|
|
|
return_places: CallReturnPlaces<'_, 'tcx>,
|
2020-03-25 16:19:14 -07:00
|
|
|
) {
|
2021-08-30 01:23:33 +01:00
|
|
|
return_places.for_each(|place| {
|
|
|
|
if let Some(local) = place.as_local() {
|
|
|
|
trans.kill(local);
|
|
|
|
}
|
|
|
|
});
|
2020-03-25 16:19:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn yield_resume_effect(
|
|
|
|
&self,
|
|
|
|
trans: &mut impl GenKill<Self::Idx>,
|
|
|
|
_resume_block: mir::BasicBlock,
|
|
|
|
resume_place: mir::Place<'tcx>,
|
|
|
|
) {
|
|
|
|
if let Some(local) = resume_place.as_local() {
|
|
|
|
trans.kill(local);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TransferFunction<'a, T>(&'a mut T);
|
|
|
|
|
|
|
|
impl<'tcx, T> Visitor<'tcx> for TransferFunction<'_, T>
|
|
|
|
where
|
|
|
|
T: GenKill<Local>,
|
|
|
|
{
|
2020-06-26 11:30:14 -07:00
|
|
|
fn visit_place(&mut self, place: &mir::Place<'tcx>, context: PlaceContext, location: Location) {
|
|
|
|
let mir::Place { projection, local } = *place;
|
|
|
|
|
|
|
|
// We purposefully do not call `super_place` here to avoid calling `visit_local` for this
|
|
|
|
// place with one of the `Projection` variants of `PlaceContext`.
|
2021-02-16 14:20:36 +05:30
|
|
|
self.visit_projection(place.as_ref(), context, location);
|
2020-06-26 11:30:14 -07:00
|
|
|
|
|
|
|
match DefUse::for_place(context) {
|
|
|
|
// Treat derefs as a use of the base local. `*p = 4` is not a def of `p` but a use.
|
|
|
|
Some(_) if place.is_indirect() => self.0.gen(local),
|
|
|
|
|
|
|
|
Some(DefUse::Def) if projection.is_empty() => self.0.kill(local),
|
|
|
|
Some(DefUse::Use) => self.0.gen(local),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 16:19:14 -07:00
|
|
|
fn visit_local(&mut self, &local: &Local, context: PlaceContext, _: Location) {
|
2020-06-26 11:30:14 -07:00
|
|
|
// Because we do not call `super_place` above, `visit_local` is only called for locals that
|
|
|
|
// do not appear as part of a `Place` in the MIR. This handles cases like the implicit use
|
|
|
|
// of the return place in a `Return` terminator or the index in an `Index` projection.
|
2020-03-25 16:19:14 -07:00
|
|
|
match DefUse::for_place(context) {
|
|
|
|
Some(DefUse::Def) => self.0.kill(local),
|
|
|
|
Some(DefUse::Use) => self.0.gen(local),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Eq, PartialEq, Clone)]
|
|
|
|
enum DefUse {
|
|
|
|
Def,
|
|
|
|
Use,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DefUse {
|
|
|
|
fn for_place(context: PlaceContext) -> Option<DefUse> {
|
|
|
|
match context {
|
|
|
|
PlaceContext::NonUse(_) => None,
|
|
|
|
|
|
|
|
PlaceContext::MutatingUse(MutatingUseContext::Store) => Some(DefUse::Def),
|
|
|
|
|
|
|
|
// `MutatingUseContext::Call` and `MutatingUseContext::Yield` indicate that this is the
|
|
|
|
// destination place for a `Call` return or `Yield` resume respectively. Since this is
|
2020-10-28 19:31:16 +01:00
|
|
|
// only a `Def` when the function returns successfully, we handle this case separately
|
2020-03-25 16:19:14 -07:00
|
|
|
// in `call_return_effect` above.
|
2021-08-30 01:23:33 +01:00
|
|
|
PlaceContext::MutatingUse(
|
|
|
|
MutatingUseContext::Call
|
|
|
|
| MutatingUseContext::AsmOutput
|
|
|
|
| MutatingUseContext::Yield,
|
|
|
|
) => None,
|
2020-03-25 16:19:14 -07:00
|
|
|
|
|
|
|
// All other contexts are uses...
|
|
|
|
PlaceContext::MutatingUse(
|
|
|
|
MutatingUseContext::AddressOf
|
|
|
|
| MutatingUseContext::Borrow
|
|
|
|
| MutatingUseContext::Drop
|
|
|
|
| MutatingUseContext::Retag,
|
|
|
|
)
|
|
|
|
| PlaceContext::NonMutatingUse(
|
|
|
|
NonMutatingUseContext::AddressOf
|
|
|
|
| NonMutatingUseContext::Copy
|
|
|
|
| NonMutatingUseContext::Inspect
|
|
|
|
| NonMutatingUseContext::Move
|
|
|
|
| NonMutatingUseContext::ShallowBorrow
|
|
|
|
| NonMutatingUseContext::SharedBorrow
|
|
|
|
| NonMutatingUseContext::UniqueBorrow,
|
|
|
|
) => Some(DefUse::Use),
|
2020-06-26 11:30:14 -07:00
|
|
|
|
|
|
|
PlaceContext::MutatingUse(MutatingUseContext::Projection)
|
|
|
|
| PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection) => {
|
|
|
|
unreachable!("A projection could be a def or a use and must be handled separately")
|
|
|
|
}
|
2020-03-25 16:19:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|