Merge AnalysisDomain
into Analysis
.
With `GenKillAnalysis` gone, there is no need for them to be separate.
This commit is contained in:
parent
4dc1b4d0b1
commit
ba13775319
11 changed files with 62 additions and 105 deletions
|
@ -16,14 +16,13 @@ use {rustc_ast as ast, rustc_graphviz as dot};
|
|||
|
||||
use super::fmt::DebugWithContext;
|
||||
use super::{
|
||||
Analysis, AnalysisDomain, Direction, JoinSemiLattice, ResultsCursor, ResultsVisitor, graphviz,
|
||||
visit_results,
|
||||
Analysis, Direction, JoinSemiLattice, ResultsCursor, ResultsVisitor, graphviz, visit_results,
|
||||
};
|
||||
use crate::errors::{
|
||||
DuplicateValuesFor, PathMustEndInFilename, RequiresAnArgument, UnknownFormatter,
|
||||
};
|
||||
|
||||
type EntrySets<'tcx, A> = IndexVec<BasicBlock, <A as AnalysisDomain<'tcx>>::Domain>;
|
||||
type EntrySets<'tcx, A> = IndexVec<BasicBlock, <A as Analysis<'tcx>>::Domain>;
|
||||
|
||||
/// A dataflow analysis that has converged to fixpoint.
|
||||
#[derive(Clone)]
|
||||
|
|
|
@ -89,11 +89,26 @@ impl<T: Idx> BitSetExt<T> for ChunkedBitSet<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Defines the domain of a dataflow problem.
|
||||
/// A dataflow problem with an arbitrarily complex transfer function.
|
||||
///
|
||||
/// This trait specifies the lattice on which this analysis operates (the domain) as well as its
|
||||
/// initial value at the entry point of each basic block.
|
||||
pub trait AnalysisDomain<'tcx> {
|
||||
/// This trait specifies the lattice on which this analysis operates (the domain), its
|
||||
/// initial value at the entry point of each basic block, and various operations.
|
||||
///
|
||||
/// # Convergence
|
||||
///
|
||||
/// When implementing this trait it's possible to choose a transfer function such that the analysis
|
||||
/// does not reach fixpoint. To guarantee convergence, your transfer functions must maintain the
|
||||
/// following invariant:
|
||||
///
|
||||
/// > If the dataflow state **before** some point in the program changes to be greater
|
||||
/// than the prior state **before** that point, the dataflow state **after** that point must
|
||||
/// also change to be greater than the prior state **after** that point.
|
||||
///
|
||||
/// This invariant guarantees that the dataflow state at a given point in the program increases
|
||||
/// monotonically until fixpoint is reached. Note that this monotonicity requirement only applies
|
||||
/// to the same point in the program at different points in time. The dataflow state at a given
|
||||
/// point in the program may or may not be greater than the state at any preceding point.
|
||||
pub trait Analysis<'tcx> {
|
||||
/// The type that holds the dataflow state at any given point in the program.
|
||||
type Domain: Clone + JoinSemiLattice;
|
||||
|
||||
|
@ -118,25 +133,7 @@ pub trait AnalysisDomain<'tcx> {
|
|||
// block where control flow could exit the MIR body (e.g., those terminated with `return` or
|
||||
// `resume`). It's not obvious how to handle `yield` points in coroutines, however.
|
||||
fn initialize_start_block(&self, body: &mir::Body<'tcx>, state: &mut Self::Domain);
|
||||
}
|
||||
|
||||
/// A dataflow problem with an arbitrarily complex transfer function.
|
||||
///
|
||||
/// # Convergence
|
||||
///
|
||||
/// When implementing this trait it's possible to choose a transfer function such that the analysis
|
||||
/// does not reach fixpoint. To guarantee convergence, your transfer functions must maintain the
|
||||
/// following invariant:
|
||||
///
|
||||
/// > If the dataflow state **before** some point in the program changes to be greater
|
||||
/// than the prior state **before** that point, the dataflow state **after** that point must
|
||||
/// also change to be greater than the prior state **after** that point.
|
||||
///
|
||||
/// This invariant guarantees that the dataflow state at a given point in the program increases
|
||||
/// monotonically until fixpoint is reached. Note that this monotonicity requirement only applies
|
||||
/// to the same point in the program at different points in time. The dataflow state at a given
|
||||
/// point in the program may or may not be greater than the state at any preceding point.
|
||||
pub trait Analysis<'tcx>: AnalysisDomain<'tcx> {
|
||||
/// Updates the current dataflow state with the effect of evaluating a statement.
|
||||
fn apply_statement_effect(
|
||||
&mut self,
|
||||
|
|
|
@ -154,7 +154,7 @@ impl<D: Direction> MockAnalysis<'_, D> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'tcx, D: Direction> AnalysisDomain<'tcx> for MockAnalysis<'tcx, D> {
|
||||
impl<'tcx, D: Direction> Analysis<'tcx> for MockAnalysis<'tcx, D> {
|
||||
type Domain = BitSet<usize>;
|
||||
type Direction = D;
|
||||
|
||||
|
@ -167,9 +167,7 @@ impl<'tcx, D: Direction> AnalysisDomain<'tcx> for MockAnalysis<'tcx, D> {
|
|||
fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut Self::Domain) {
|
||||
unimplemented!("This is never called since `MockAnalysis` is never iterated to fixpoint");
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx, D: Direction> Analysis<'tcx> for MockAnalysis<'tcx, D> {
|
||||
fn apply_statement_effect(
|
||||
&mut self,
|
||||
state: &mut Self::Domain,
|
||||
|
|
|
@ -2,7 +2,7 @@ use rustc_index::bit_set::BitSet;
|
|||
use rustc_middle::mir::visit::Visitor;
|
||||
use rustc_middle::mir::*;
|
||||
|
||||
use crate::{Analysis, AnalysisDomain, GenKill};
|
||||
use crate::{Analysis, GenKill};
|
||||
|
||||
/// A dataflow analysis that tracks whether a pointer or reference could possibly exist that points
|
||||
/// to a given local. This analysis ignores fake borrows, so it should not be used by
|
||||
|
@ -20,7 +20,7 @@ impl MaybeBorrowedLocals {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'tcx> AnalysisDomain<'tcx> for MaybeBorrowedLocals {
|
||||
impl<'tcx> Analysis<'tcx> for MaybeBorrowedLocals {
|
||||
type Domain = BitSet<Local>;
|
||||
const NAME: &'static str = "maybe_borrowed_locals";
|
||||
|
||||
|
@ -32,9 +32,7 @@ impl<'tcx> AnalysisDomain<'tcx> for MaybeBorrowedLocals {
|
|||
fn initialize_start_block(&self, _: &Body<'tcx>, _: &mut Self::Domain) {
|
||||
// No locals are aliased on function entry
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> Analysis<'tcx> for MaybeBorrowedLocals {
|
||||
fn apply_statement_effect(
|
||||
&mut self,
|
||||
trans: &mut Self::Domain,
|
||||
|
|
|
@ -11,9 +11,8 @@ use crate::elaborate_drops::DropFlagState;
|
|||
use crate::framework::SwitchIntEdgeEffects;
|
||||
use crate::move_paths::{HasMoveData, InitIndex, InitKind, LookupResult, MoveData, MovePathIndex};
|
||||
use crate::{
|
||||
Analysis, AnalysisDomain, GenKill, MaybeReachable, drop_flag_effects,
|
||||
drop_flag_effects_for_function_entry, drop_flag_effects_for_location, lattice,
|
||||
on_all_children_bits, on_lookup_result_bits,
|
||||
Analysis, GenKill, MaybeReachable, drop_flag_effects, drop_flag_effects_for_function_entry,
|
||||
drop_flag_effects_for_location, lattice, on_all_children_bits, on_lookup_result_bits,
|
||||
};
|
||||
|
||||
/// `MaybeInitializedPlaces` tracks all places that might be
|
||||
|
@ -270,7 +269,7 @@ impl<'tcx> HasMoveData<'tcx> for EverInitializedPlaces<'_, 'tcx> {
|
|||
|
||||
impl<'a, 'tcx> MaybeInitializedPlaces<'a, 'tcx> {
|
||||
fn update_bits(
|
||||
trans: &mut <Self as AnalysisDomain<'tcx>>::Domain,
|
||||
trans: &mut <Self as Analysis<'tcx>>::Domain,
|
||||
path: MovePathIndex,
|
||||
state: DropFlagState,
|
||||
) {
|
||||
|
@ -283,7 +282,7 @@ impl<'a, 'tcx> MaybeInitializedPlaces<'a, 'tcx> {
|
|||
|
||||
impl<'tcx> MaybeUninitializedPlaces<'_, 'tcx> {
|
||||
fn update_bits(
|
||||
trans: &mut <Self as AnalysisDomain<'tcx>>::Domain,
|
||||
trans: &mut <Self as Analysis<'tcx>>::Domain,
|
||||
path: MovePathIndex,
|
||||
state: DropFlagState,
|
||||
) {
|
||||
|
@ -296,7 +295,7 @@ impl<'tcx> MaybeUninitializedPlaces<'_, 'tcx> {
|
|||
|
||||
impl<'a, 'tcx> DefinitelyInitializedPlaces<'a, 'tcx> {
|
||||
fn update_bits(
|
||||
trans: &mut <Self as AnalysisDomain<'tcx>>::Domain,
|
||||
trans: &mut <Self as Analysis<'tcx>>::Domain,
|
||||
path: MovePathIndex,
|
||||
state: DropFlagState,
|
||||
) {
|
||||
|
@ -307,7 +306,7 @@ impl<'a, 'tcx> DefinitelyInitializedPlaces<'a, 'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'tcx> AnalysisDomain<'tcx> for MaybeInitializedPlaces<'_, 'tcx> {
|
||||
impl<'tcx> Analysis<'tcx> for MaybeInitializedPlaces<'_, 'tcx> {
|
||||
/// There can be many more `MovePathIndex` than there are locals in a MIR body.
|
||||
/// We use a chunked bitset to avoid paying too high a memory footprint.
|
||||
type Domain = MaybeReachable<ChunkedBitSet<MovePathIndex>>;
|
||||
|
@ -327,9 +326,7 @@ impl<'tcx> AnalysisDomain<'tcx> for MaybeInitializedPlaces<'_, 'tcx> {
|
|||
state.gen_(path);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> Analysis<'tcx> for MaybeInitializedPlaces<'_, 'tcx> {
|
||||
fn apply_statement_effect(
|
||||
&mut self,
|
||||
trans: &mut Self::Domain,
|
||||
|
@ -436,7 +433,7 @@ impl<'tcx> Analysis<'tcx> for MaybeInitializedPlaces<'_, 'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'tcx> AnalysisDomain<'tcx> for MaybeUninitializedPlaces<'_, 'tcx> {
|
||||
impl<'tcx> Analysis<'tcx> for MaybeUninitializedPlaces<'_, 'tcx> {
|
||||
/// There can be many more `MovePathIndex` than there are locals in a MIR body.
|
||||
/// We use a chunked bitset to avoid paying too high a memory footprint.
|
||||
type Domain = ChunkedBitSet<MovePathIndex>;
|
||||
|
@ -458,9 +455,7 @@ impl<'tcx> AnalysisDomain<'tcx> for MaybeUninitializedPlaces<'_, 'tcx> {
|
|||
state.remove(path);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> Analysis<'tcx> for MaybeUninitializedPlaces<'_, 'tcx> {
|
||||
fn apply_statement_effect(
|
||||
&mut self,
|
||||
trans: &mut Self::Domain,
|
||||
|
@ -559,7 +554,7 @@ impl<'tcx> Analysis<'tcx> for MaybeUninitializedPlaces<'_, 'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> AnalysisDomain<'tcx> for DefinitelyInitializedPlaces<'a, 'tcx> {
|
||||
impl<'a, 'tcx> Analysis<'tcx> for DefinitelyInitializedPlaces<'a, 'tcx> {
|
||||
/// Use set intersection as the join operator.
|
||||
type Domain = lattice::Dual<BitSet<MovePathIndex>>;
|
||||
|
||||
|
@ -579,9 +574,7 @@ impl<'a, 'tcx> AnalysisDomain<'tcx> for DefinitelyInitializedPlaces<'a, 'tcx> {
|
|||
state.0.insert(path);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> Analysis<'tcx> for DefinitelyInitializedPlaces<'_, 'tcx> {
|
||||
fn apply_statement_effect(
|
||||
&mut self,
|
||||
trans: &mut Self::Domain,
|
||||
|
@ -625,7 +618,7 @@ impl<'tcx> Analysis<'tcx> for DefinitelyInitializedPlaces<'_, 'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'tcx> AnalysisDomain<'tcx> for EverInitializedPlaces<'_, 'tcx> {
|
||||
impl<'tcx> Analysis<'tcx> for EverInitializedPlaces<'_, 'tcx> {
|
||||
/// There can be many more `InitIndex` than there are locals in a MIR body.
|
||||
/// We use a chunked bitset to avoid paying too high a memory footprint.
|
||||
type Domain = ChunkedBitSet<InitIndex>;
|
||||
|
@ -642,9 +635,7 @@ impl<'tcx> AnalysisDomain<'tcx> for EverInitializedPlaces<'_, 'tcx> {
|
|||
state.insert(InitIndex::new(arg_init));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> Analysis<'tcx> for EverInitializedPlaces<'_, 'tcx> {
|
||||
#[instrument(skip(self, trans), level = "debug")]
|
||||
fn apply_statement_effect(
|
||||
&mut self,
|
||||
|
|
|
@ -4,7 +4,7 @@ use rustc_middle::mir::{
|
|||
self, CallReturnPlaces, Local, Location, Place, StatementKind, TerminatorEdges,
|
||||
};
|
||||
|
||||
use crate::{Analysis, AnalysisDomain, Backward, GenKill};
|
||||
use crate::{Analysis, Backward, GenKill};
|
||||
|
||||
/// A [live-variable dataflow analysis][liveness].
|
||||
///
|
||||
|
@ -25,7 +25,7 @@ use crate::{Analysis, AnalysisDomain, Backward, GenKill};
|
|||
/// [liveness]: https://en.wikipedia.org/wiki/Live_variable_analysis
|
||||
pub struct MaybeLiveLocals;
|
||||
|
||||
impl<'tcx> AnalysisDomain<'tcx> for MaybeLiveLocals {
|
||||
impl<'tcx> Analysis<'tcx> for MaybeLiveLocals {
|
||||
type Domain = BitSet<Local>;
|
||||
type Direction = Backward;
|
||||
|
||||
|
@ -39,9 +39,7 @@ impl<'tcx> AnalysisDomain<'tcx> for MaybeLiveLocals {
|
|||
fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut Self::Domain) {
|
||||
// No variables are live until we observe a use
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> Analysis<'tcx> for MaybeLiveLocals {
|
||||
fn apply_statement_effect(
|
||||
&mut self,
|
||||
trans: &mut Self::Domain,
|
||||
|
@ -219,7 +217,7 @@ impl<'a> MaybeTransitiveLiveLocals<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> AnalysisDomain<'tcx> for MaybeTransitiveLiveLocals<'a> {
|
||||
impl<'a, 'tcx> Analysis<'tcx> for MaybeTransitiveLiveLocals<'a> {
|
||||
type Domain = BitSet<Local>;
|
||||
type Direction = Backward;
|
||||
|
||||
|
@ -233,9 +231,7 @@ impl<'a, 'tcx> AnalysisDomain<'tcx> for MaybeTransitiveLiveLocals<'a> {
|
|||
fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut Self::Domain) {
|
||||
// No variables are live until we observe a use
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> Analysis<'tcx> for MaybeTransitiveLiveLocals<'a> {
|
||||
fn apply_statement_effect(
|
||||
&mut self,
|
||||
trans: &mut Self::Domain,
|
||||
|
|
|
@ -5,7 +5,7 @@ use rustc_middle::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor};
|
|||
use rustc_middle::mir::*;
|
||||
|
||||
use super::MaybeBorrowedLocals;
|
||||
use crate::{Analysis, AnalysisDomain, GenKill, ResultsCursor};
|
||||
use crate::{Analysis, GenKill, ResultsCursor};
|
||||
|
||||
pub struct MaybeStorageLive<'a> {
|
||||
always_live_locals: Cow<'a, BitSet<Local>>,
|
||||
|
@ -17,7 +17,7 @@ impl<'a> MaybeStorageLive<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> AnalysisDomain<'tcx> for MaybeStorageLive<'a> {
|
||||
impl<'a, 'tcx> Analysis<'tcx> for MaybeStorageLive<'a> {
|
||||
type Domain = BitSet<Local>;
|
||||
|
||||
const NAME: &'static str = "maybe_storage_live";
|
||||
|
@ -37,9 +37,7 @@ impl<'a, 'tcx> AnalysisDomain<'tcx> for MaybeStorageLive<'a> {
|
|||
on_entry.insert(arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> Analysis<'tcx> for MaybeStorageLive<'a> {
|
||||
fn apply_statement_effect(
|
||||
&mut self,
|
||||
trans: &mut Self::Domain,
|
||||
|
@ -83,7 +81,7 @@ impl<'a> MaybeStorageDead<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> AnalysisDomain<'tcx> for MaybeStorageDead<'a> {
|
||||
impl<'a, 'tcx> Analysis<'tcx> for MaybeStorageDead<'a> {
|
||||
type Domain = BitSet<Local>;
|
||||
|
||||
const NAME: &'static str = "maybe_storage_dead";
|
||||
|
@ -102,9 +100,7 @@ impl<'a, 'tcx> AnalysisDomain<'tcx> for MaybeStorageDead<'a> {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> Analysis<'tcx> for MaybeStorageDead<'a> {
|
||||
fn apply_statement_effect(
|
||||
&mut self,
|
||||
trans: &mut Self::Domain,
|
||||
|
@ -152,7 +148,7 @@ impl<'mir, 'tcx> MaybeRequiresStorage<'mir, 'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'tcx> AnalysisDomain<'tcx> for MaybeRequiresStorage<'_, 'tcx> {
|
||||
impl<'tcx> Analysis<'tcx> for MaybeRequiresStorage<'_, 'tcx> {
|
||||
type Domain = BitSet<Local>;
|
||||
|
||||
const NAME: &'static str = "requires_storage";
|
||||
|
@ -169,9 +165,7 @@ impl<'tcx> AnalysisDomain<'tcx> for MaybeRequiresStorage<'_, 'tcx> {
|
|||
on_entry.insert(arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> Analysis<'tcx> for MaybeRequiresStorage<'_, 'tcx> {
|
||||
fn apply_before_statement_effect(
|
||||
&mut self,
|
||||
trans: &mut Self::Domain,
|
||||
|
@ -327,11 +321,7 @@ impl<'tcx> Analysis<'tcx> for MaybeRequiresStorage<'_, 'tcx> {
|
|||
|
||||
impl<'tcx> MaybeRequiresStorage<'_, 'tcx> {
|
||||
/// Kill locals that are fully moved and have not been borrowed.
|
||||
fn check_for_move(
|
||||
&mut self,
|
||||
trans: &mut <Self as AnalysisDomain<'tcx>>::Domain,
|
||||
loc: Location,
|
||||
) {
|
||||
fn check_for_move(&mut self, trans: &mut <Self as Analysis<'tcx>>::Domain, loc: Location) {
|
||||
let body = self.borrowed_locals.body();
|
||||
let mut visitor = MoveVisitor { trans, borrowed_locals: &mut self.borrowed_locals };
|
||||
visitor.visit_location(body, loc);
|
||||
|
|
|
@ -18,9 +18,9 @@ pub use self::drop_flag_effects::{
|
|||
move_path_children_matching, on_all_children_bits, on_lookup_result_bits,
|
||||
};
|
||||
pub use self::framework::{
|
||||
Analysis, AnalysisDomain, Backward, Direction, Engine, Forward, GenKill, JoinSemiLattice,
|
||||
MaybeReachable, Results, ResultsCursor, ResultsVisitable, ResultsVisitor, SwitchIntEdgeEffects,
|
||||
fmt, graphviz, lattice, visit_results,
|
||||
Analysis, Backward, Direction, Engine, Forward, GenKill, JoinSemiLattice, MaybeReachable,
|
||||
Results, ResultsCursor, ResultsVisitable, ResultsVisitor, SwitchIntEdgeEffects, fmt, graphviz,
|
||||
lattice, visit_results,
|
||||
};
|
||||
use self::move_paths::MoveData;
|
||||
|
||||
|
|
|
@ -51,7 +51,7 @@ use tracing::debug;
|
|||
|
||||
use crate::fmt::DebugWithContext;
|
||||
use crate::lattice::{HasBottom, HasTop};
|
||||
use crate::{Analysis, AnalysisDomain, JoinSemiLattice, SwitchIntEdgeEffects};
|
||||
use crate::{Analysis, JoinSemiLattice, SwitchIntEdgeEffects};
|
||||
|
||||
pub trait ValueAnalysis<'tcx> {
|
||||
/// For each place of interest, the analysis tracks a value of the given type.
|
||||
|
@ -334,7 +334,7 @@ pub trait ValueAnalysis<'tcx> {
|
|||
|
||||
pub struct ValueAnalysisWrapper<T>(pub T);
|
||||
|
||||
impl<'tcx, T: ValueAnalysis<'tcx>> AnalysisDomain<'tcx> for ValueAnalysisWrapper<T> {
|
||||
impl<'tcx, T: ValueAnalysis<'tcx>> Analysis<'tcx> for ValueAnalysisWrapper<T> {
|
||||
type Domain = State<T::Value>;
|
||||
|
||||
const NAME: &'static str = T::NAME;
|
||||
|
@ -351,12 +351,7 @@ impl<'tcx, T: ValueAnalysis<'tcx>> AnalysisDomain<'tcx> for ValueAnalysisWrapper
|
|||
state.flood(PlaceRef { local: arg, projection: &[] }, self.0.map());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx, T> Analysis<'tcx> for ValueAnalysisWrapper<T>
|
||||
where
|
||||
T: ValueAnalysis<'tcx>,
|
||||
{
|
||||
fn apply_statement_effect(
|
||||
&mut self,
|
||||
state: &mut Self::Domain,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue