summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_mir_dataflow/src/framework
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_mir_dataflow/src/framework')
-rw-r--r--compiler/rustc_mir_dataflow/src/framework/cursor.rs108
-rw-r--r--compiler/rustc_mir_dataflow/src/framework/direction.rs22
-rw-r--r--compiler/rustc_mir_dataflow/src/framework/engine.rs140
-rw-r--r--compiler/rustc_mir_dataflow/src/framework/graphviz.rs67
-rw-r--r--compiler/rustc_mir_dataflow/src/framework/lattice.rs2
-rw-r--r--compiler/rustc_mir_dataflow/src/framework/mod.rs40
-rw-r--r--compiler/rustc_mir_dataflow/src/framework/tests.rs5
-rw-r--r--compiler/rustc_mir_dataflow/src/framework/visitor.rs29
8 files changed, 132 insertions, 281 deletions
diff --git a/compiler/rustc_mir_dataflow/src/framework/cursor.rs b/compiler/rustc_mir_dataflow/src/framework/cursor.rs
index c978bddfe..815f20459 100644
--- a/compiler/rustc_mir_dataflow/src/framework/cursor.rs
+++ b/compiler/rustc_mir_dataflow/src/framework/cursor.rs
@@ -1,60 +1,14 @@
//! Random access inspection of the results of a dataflow analysis.
-use crate::{framework::BitSetExt, CloneAnalysis};
+use crate::framework::BitSetExt;
-use std::borrow::{Borrow, BorrowMut};
use std::cmp::Ordering;
#[cfg(debug_assertions)]
use rustc_index::bit_set::BitSet;
use rustc_middle::mir::{self, BasicBlock, Location};
-use super::{Analysis, Direction, Effect, EffectIndex, EntrySets, Results, ResultsCloned};
-
-// `AnalysisResults` is needed as an impl such as the following has an unconstrained type
-// parameter:
-// ```
-// impl<'tcx, A, E, R> ResultsCursor<'_, 'tcx, A, R>
-// where
-// A: Analysis<'tcx>,
-// E: Borrow<EntrySets<'tcx, A>>,
-// R: Results<'tcx, A, E>,
-// {}
-// ```
-
-/// A type representing the analysis results consumed by a `ResultsCursor`.
-pub trait AnalysisResults<'tcx, A>: BorrowMut<Results<'tcx, A, Self::EntrySets>>
-where
- A: Analysis<'tcx>,
-{
- /// The type containing the entry sets for this `Results` type.
- ///
- /// Should be either `EntrySets<'tcx, A>` or `&EntrySets<'tcx, A>`.
- type EntrySets: Borrow<EntrySets<'tcx, A>>;
-}
-impl<'tcx, A, E> AnalysisResults<'tcx, A> for Results<'tcx, A, E>
-where
- A: Analysis<'tcx>,
- E: Borrow<EntrySets<'tcx, A>>,
-{
- type EntrySets = E;
-}
-impl<'a, 'tcx, A, E> AnalysisResults<'tcx, A> for &'a mut Results<'tcx, A, E>
-where
- A: Analysis<'tcx>,
- E: Borrow<EntrySets<'tcx, A>>,
-{
- type EntrySets = E;
-}
-
-/// A `ResultsCursor` that borrows the underlying `Results`.
-pub type ResultsRefCursor<'res, 'mir, 'tcx, A> =
- ResultsCursor<'mir, 'tcx, A, &'res mut Results<'tcx, A>>;
-
-/// A `ResultsCursor` which uses a cloned `Analysis` while borrowing the underlying `Results`. This
-/// allows multiple cursors over the same `Results`.
-pub type ResultsClonedCursor<'res, 'mir, 'tcx, A> =
- ResultsCursor<'mir, 'tcx, A, ResultsCloned<'res, 'tcx, A>>;
+use super::{Analysis, Direction, Effect, EffectIndex, Results};
/// Allows random access inspection of the results of a dataflow analysis.
///
@@ -62,15 +16,12 @@ pub type ResultsClonedCursor<'res, 'mir, 'tcx, A> =
/// the same order as the `DIRECTION` of the analysis. In the worst case—when statements are
/// visited in *reverse* order—performance will be quadratic in the number of statements in the
/// block. The order in which basic blocks are inspected has no impact on performance.
-///
-/// A `ResultsCursor` can either own (the default) or borrow the dataflow results it inspects. The
-/// type of ownership is determined by `R` (see `ResultsRefCursor` above).
-pub struct ResultsCursor<'mir, 'tcx, A, R = Results<'tcx, A>>
+pub struct ResultsCursor<'mir, 'tcx, A>
where
A: Analysis<'tcx>,
{
body: &'mir mir::Body<'tcx>,
- results: R,
+ results: Results<'tcx, A>,
state: A::Domain,
pos: CursorPosition,
@@ -84,7 +35,7 @@ where
reachable_blocks: BitSet<BasicBlock>,
}
-impl<'mir, 'tcx, A, R> ResultsCursor<'mir, 'tcx, A, R>
+impl<'mir, 'tcx, A> ResultsCursor<'mir, 'tcx, A>
where
A: Analysis<'tcx>,
{
@@ -99,30 +50,13 @@ where
}
/// Unwraps this cursor, returning the underlying `Results`.
- pub fn into_results(self) -> R {
+ pub fn into_results(self) -> Results<'tcx, A> {
self.results
}
-}
-impl<'res, 'mir, 'tcx, A> ResultsCursor<'mir, 'tcx, A, ResultsCloned<'res, 'tcx, A>>
-where
- A: Analysis<'tcx> + CloneAnalysis,
-{
- /// Creates a new cursor over the same `Results`. Note that the cursor's position is *not*
- /// copied.
- pub fn new_cursor(&self) -> Self {
- Self::new(self.body, self.results.reclone_analysis())
- }
-}
-
-impl<'mir, 'tcx, A, R> ResultsCursor<'mir, 'tcx, A, R>
-where
- A: Analysis<'tcx>,
- R: AnalysisResults<'tcx, A>,
-{
/// Returns a new cursor that can inspect `results`.
- pub fn new(body: &'mir mir::Body<'tcx>, results: R) -> Self {
- let bottom_value = results.borrow().analysis.bottom_value(body);
+ pub fn new(body: &'mir mir::Body<'tcx>, results: Results<'tcx, A>) -> Self {
+ let bottom_value = results.analysis.bottom_value(body);
ResultsCursor {
body,
results,
@@ -147,28 +81,23 @@ where
}
/// Returns the underlying `Results`.
- pub fn results(&mut self) -> &Results<'tcx, A, R::EntrySets> {
- self.results.borrow()
+ pub fn results(&self) -> &Results<'tcx, A> {
+ &self.results
}
/// Returns the underlying `Results`.
- pub fn mut_results(&mut self) -> &mut Results<'tcx, A, R::EntrySets> {
- self.results.borrow_mut()
+ pub fn mut_results(&mut self) -> &mut Results<'tcx, A> {
+ &mut self.results
}
/// Returns the `Analysis` used to generate the underlying `Results`.
pub fn analysis(&self) -> &A {
- &self.results.borrow().analysis
+ &self.results.analysis
}
/// Returns the `Analysis` used to generate the underlying `Results`.
pub fn mut_analysis(&mut self) -> &mut A {
- &mut self.results.borrow_mut().analysis
- }
-
- /// Returns both the dataflow state at the current location and the `Analysis`.
- pub fn get_with_analysis(&mut self) -> (&A::Domain, &mut A) {
- (&self.state, &mut self.results.borrow_mut().analysis)
+ &mut self.results.analysis
}
/// Resets the cursor to hold the entry set for the given basic block.
@@ -180,7 +109,7 @@ where
#[cfg(debug_assertions)]
assert!(self.reachable_blocks.contains(block));
- self.state.clone_from(self.results.borrow().entry_set_for_block(block));
+ self.state.clone_from(self.results.entry_set_for_block(block));
self.pos = CursorPosition::block_entry(block);
self.state_needs_reset = false;
}
@@ -269,11 +198,10 @@ where
)
};
- let analysis = &mut self.results.borrow_mut().analysis;
let target_effect_index = effect.at_index(target.statement_index);
A::Direction::apply_effects_in_range(
- analysis,
+ &mut self.results.analysis,
&mut self.state,
target.block,
block_data,
@@ -289,12 +217,12 @@ where
/// This can be used, e.g., to apply the call return effect directly to the cursor without
/// creating an extra copy of the dataflow state.
pub fn apply_custom_effect(&mut self, f: impl FnOnce(&mut A, &mut A::Domain)) {
- f(&mut self.results.borrow_mut().analysis, &mut self.state);
+ f(&mut self.results.analysis, &mut self.state);
self.state_needs_reset = true;
}
}
-impl<'mir, 'tcx, A, R> ResultsCursor<'mir, 'tcx, A, R>
+impl<'mir, 'tcx, A> ResultsCursor<'mir, 'tcx, A>
where
A: crate::GenKillAnalysis<'tcx>,
A::Domain: BitSetExt<A::Idx>,
diff --git a/compiler/rustc_mir_dataflow/src/framework/direction.rs b/compiler/rustc_mir_dataflow/src/framework/direction.rs
index 70451edd5..4c3fadf48 100644
--- a/compiler/rustc_mir_dataflow/src/framework/direction.rs
+++ b/compiler/rustc_mir_dataflow/src/framework/direction.rs
@@ -196,7 +196,7 @@ impl Direction for Backward {
{
results.reset_to_block_entry(state, block);
- vis.visit_block_end(results, &state, block_data, block);
+ vis.visit_block_end(state);
// Terminator
let loc = Location { block, statement_index: block_data.statements.len() };
@@ -214,7 +214,7 @@ impl Direction for Backward {
vis.visit_statement_after_primary_effect(results, state, stmt, loc);
}
- vis.visit_block_start(results, state, block_data, block);
+ vis.visit_block_start(state);
}
fn join_state_into_successors_of<'tcx, A>(
@@ -287,12 +287,12 @@ impl Direction for Backward {
}
}
-struct BackwardSwitchIntEdgeEffectsApplier<'a, 'tcx, D, F> {
- body: &'a mir::Body<'tcx>,
+struct BackwardSwitchIntEdgeEffectsApplier<'mir, 'tcx, D, F> {
+ body: &'mir mir::Body<'tcx>,
pred: BasicBlock,
- exit_state: &'a mut D,
+ exit_state: &'mir mut D,
bb: BasicBlock,
- propagate: &'a mut F,
+ propagate: &'mir mut F,
effects_applied: bool,
}
@@ -449,7 +449,7 @@ impl Direction for Forward {
{
results.reset_to_block_entry(state, block);
- vis.visit_block_start(results, state, block_data, block);
+ vis.visit_block_start(state);
for (statement_index, stmt) in block_data.statements.iter().enumerate() {
let loc = Location { block, statement_index };
@@ -466,7 +466,7 @@ impl Direction for Forward {
results.reconstruct_terminator_effect(state, term, loc);
vis.visit_terminator_after_primary_effect(results, state, term, loc);
- vis.visit_block_end(results, state, block_data, block);
+ vis.visit_block_end(state);
}
fn join_state_into_successors_of<'tcx, A>(
@@ -523,9 +523,9 @@ impl Direction for Forward {
}
}
-struct ForwardSwitchIntEdgeEffectsApplier<'a, D, F> {
- exit_state: &'a mut D,
- targets: &'a SwitchTargets,
+struct ForwardSwitchIntEdgeEffectsApplier<'mir, D, F> {
+ exit_state: &'mir mut D,
+ targets: &'mir SwitchTargets,
propagate: F,
effects_applied: bool,
diff --git a/compiler/rustc_mir_dataflow/src/framework/engine.rs b/compiler/rustc_mir_dataflow/src/framework/engine.rs
index a29962d77..784872c7e 100644
--- a/compiler/rustc_mir_dataflow/src/framework/engine.rs
+++ b/compiler/rustc_mir_dataflow/src/framework/engine.rs
@@ -5,9 +5,7 @@ use crate::errors::{
};
use crate::framework::BitSetExt;
-use std::borrow::Borrow;
use std::ffi::OsString;
-use std::marker::PhantomData;
use std::path::PathBuf;
use rustc_ast as ast;
@@ -24,42 +22,37 @@ use rustc_span::symbol::{sym, Symbol};
use super::fmt::DebugWithContext;
use super::graphviz;
use super::{
- visit_results, Analysis, AnalysisDomain, CloneAnalysis, Direction, GenKill, GenKillAnalysis,
- GenKillSet, JoinSemiLattice, ResultsClonedCursor, ResultsCursor, ResultsRefCursor,
- ResultsVisitor,
+ visit_results, Analysis, AnalysisDomain, Direction, GenKill, GenKillAnalysis, GenKillSet,
+ JoinSemiLattice, ResultsCursor, ResultsVisitor,
};
pub type EntrySets<'tcx, A> = IndexVec<BasicBlock, <A as AnalysisDomain<'tcx>>::Domain>;
/// A dataflow analysis that has converged to fixpoint.
-pub struct Results<'tcx, A, E = EntrySets<'tcx, A>>
+#[derive(Clone)]
+pub struct Results<'tcx, A>
where
A: Analysis<'tcx>,
{
pub analysis: A,
- pub(super) entry_sets: E,
- pub(super) _marker: PhantomData<&'tcx ()>,
+ pub(super) entry_sets: EntrySets<'tcx, A>,
}
-/// `Results` type with a cloned `Analysis` and borrowed entry sets.
-pub type ResultsCloned<'res, 'tcx, A> = Results<'tcx, A, &'res EntrySets<'tcx, A>>;
-
-impl<'tcx, A, E> Results<'tcx, A, E>
+impl<'tcx, A> Results<'tcx, A>
where
A: Analysis<'tcx>,
- E: Borrow<EntrySets<'tcx, A>>,
{
/// Creates a `ResultsCursor` that can inspect these `Results`.
pub fn into_results_cursor<'mir>(
self,
body: &'mir mir::Body<'tcx>,
- ) -> ResultsCursor<'mir, 'tcx, A, Self> {
+ ) -> ResultsCursor<'mir, 'tcx, A> {
ResultsCursor::new(body, self)
}
/// Gets the dataflow state for the given block.
pub fn entry_set_for_block(&self, block: BasicBlock) -> &A::Domain {
- &self.entry_sets.borrow()[block]
+ &self.entry_sets[block]
}
pub fn visit_with<'mir>(
@@ -80,60 +73,14 @@ where
visit_results(body, blocks.map(|(bb, _)| bb), self, vis)
}
}
-impl<'tcx, A> Results<'tcx, A>
-where
- A: Analysis<'tcx>,
-{
- /// Creates a `ResultsCursor` that can inspect these `Results`.
- pub fn as_results_cursor<'a, 'mir>(
- &'a mut self,
- body: &'mir mir::Body<'tcx>,
- ) -> ResultsRefCursor<'a, 'mir, 'tcx, A> {
- ResultsCursor::new(body, self)
- }
-}
-impl<'tcx, A> Results<'tcx, A>
-where
- A: Analysis<'tcx> + CloneAnalysis,
-{
- /// Creates a new `Results` type with a cloned `Analysis` and borrowed entry sets.
- pub fn clone_analysis(&self) -> ResultsCloned<'_, 'tcx, A> {
- Results {
- analysis: self.analysis.clone_analysis(),
- entry_sets: &self.entry_sets,
- _marker: PhantomData,
- }
- }
-
- /// Creates a `ResultsCursor` that can inspect these `Results`.
- pub fn cloned_results_cursor<'mir>(
- &self,
- body: &'mir mir::Body<'tcx>,
- ) -> ResultsClonedCursor<'_, 'mir, 'tcx, A> {
- self.clone_analysis().into_results_cursor(body)
- }
-}
-impl<'res, 'tcx, A> Results<'tcx, A, &'res EntrySets<'tcx, A>>
-where
- A: Analysis<'tcx> + CloneAnalysis,
-{
- /// Creates a new `Results` type with a cloned `Analysis` and borrowed entry sets.
- pub fn reclone_analysis(&self) -> Self {
- Results {
- analysis: self.analysis.clone_analysis(),
- entry_sets: self.entry_sets,
- _marker: PhantomData,
- }
- }
-}
/// A solver for dataflow problems.
-pub struct Engine<'a, 'tcx, A>
+pub struct Engine<'mir, 'tcx, A>
where
A: Analysis<'tcx>,
{
tcx: TyCtxt<'tcx>,
- body: &'a mir::Body<'tcx>,
+ body: &'mir mir::Body<'tcx>,
entry_sets: IndexVec<BasicBlock, A::Domain>,
pass_name: Option<&'static str>,
analysis: A,
@@ -147,14 +94,14 @@ where
apply_statement_trans_for_block: Option<Box<dyn Fn(BasicBlock, &mut A::Domain)>>,
}
-impl<'a, 'tcx, A, D, T> Engine<'a, 'tcx, A>
+impl<'mir, 'tcx, A, D, T> Engine<'mir, 'tcx, A>
where
A: GenKillAnalysis<'tcx, Idx = T, Domain = D>,
D: Clone + JoinSemiLattice + GenKill<T> + BitSetExt<T>,
T: Idx,
{
/// Creates a new `Engine` to solve a gen-kill dataflow problem.
- pub fn new_gen_kill(tcx: TyCtxt<'tcx>, body: &'a mir::Body<'tcx>, mut analysis: A) -> Self {
+ pub fn new_gen_kill(tcx: TyCtxt<'tcx>, body: &'mir mir::Body<'tcx>, mut analysis: A) -> Self {
// If there are no back-edges in the control-flow graph, we only ever need to apply the
// transfer function for each block exactly once (assuming that we process blocks in RPO).
//
@@ -186,7 +133,7 @@ where
}
}
-impl<'a, 'tcx, A, D> Engine<'a, 'tcx, A>
+impl<'mir, 'tcx, A, D> Engine<'mir, 'tcx, A>
where
A: Analysis<'tcx, Domain = D>,
D: Clone + JoinSemiLattice,
@@ -196,13 +143,13 @@ where
///
/// Gen-kill problems should use `new_gen_kill`, which will coalesce transfer functions for
/// better performance.
- pub fn new_generic(tcx: TyCtxt<'tcx>, body: &'a mir::Body<'tcx>, analysis: A) -> Self {
+ pub fn new_generic(tcx: TyCtxt<'tcx>, body: &'mir mir::Body<'tcx>, analysis: A) -> Self {
Self::new(tcx, body, analysis, None)
}
fn new(
tcx: TyCtxt<'tcx>,
- body: &'a mir::Body<'tcx>,
+ body: &'mir mir::Body<'tcx>,
analysis: A,
apply_statement_trans_for_block: Option<Box<dyn Fn(BasicBlock, &mut A::Domain)>>,
) -> Self {
@@ -239,7 +186,6 @@ where
tcx,
apply_statement_trans_for_block,
pass_name,
- ..
} = self;
let mut dirty_queue: WorkQueue<BasicBlock> = WorkQueue::with_none(body.basic_blocks.len());
@@ -292,29 +238,31 @@ where
);
}
- let mut results = Results { analysis, entry_sets, _marker: PhantomData };
+ let results = Results { analysis, entry_sets };
if tcx.sess.opts.unstable_opts.dump_mir_dataflow {
- let res = write_graphviz_results(tcx, &body, &mut results, pass_name);
+ let (res, results) = write_graphviz_results(tcx, body, results, pass_name);
if let Err(e) = res {
error!("Failed to write graphviz dataflow results: {}", e);
}
+ results
+ } else {
+ results
}
-
- results
}
}
// Graphviz
/// Writes a DOT file containing the results of a dataflow analysis if the user requested it via
-/// `rustc_mir` attributes and `-Z dump-mir-dataflow`.
+/// `rustc_mir` attributes and `-Z dump-mir-dataflow`. The `Result` in and the `Results` out are
+/// the same.
fn write_graphviz_results<'tcx, A>(
tcx: TyCtxt<'tcx>,
body: &mir::Body<'tcx>,
- results: &mut Results<'tcx, A>,
+ results: Results<'tcx, A>,
pass_name: Option<&'static str>,
-) -> std::io::Result<()>
+) -> (std::io::Result<()>, Results<'tcx, A>)
where
A: Analysis<'tcx>,
A::Domain: DebugWithContext<A>,
@@ -325,23 +273,30 @@ where
let def_id = body.source.def_id();
let Ok(attrs) = RustcMirAttrs::parse(tcx, def_id) else {
// Invalid `rustc_mir` attrs are reported in `RustcMirAttrs::parse`
- return Ok(());
+ return (Ok(()), results);
};
- let mut file = match attrs.output_path(A::NAME) {
- Some(path) => {
- debug!("printing dataflow results for {:?} to {}", def_id, path.display());
- if let Some(parent) = path.parent() {
- fs::create_dir_all(parent)?;
+ let file = try {
+ match attrs.output_path(A::NAME) {
+ Some(path) => {
+ debug!("printing dataflow results for {:?} to {}", def_id, path.display());
+ if let Some(parent) = path.parent() {
+ fs::create_dir_all(parent)?;
+ }
+ let f = fs::File::create(&path)?;
+ io::BufWriter::new(f)
}
- io::BufWriter::new(fs::File::create(&path)?)
- }
- None if dump_enabled(tcx, A::NAME, def_id) => {
- create_dump_file(tcx, ".dot", false, A::NAME, &pass_name.unwrap_or("-----"), body)?
- }
+ None if dump_enabled(tcx, A::NAME, def_id) => {
+ create_dump_file(tcx, ".dot", false, A::NAME, &pass_name.unwrap_or("-----"), body)?
+ }
- _ => return Ok(()),
+ _ => return (Ok(()), results),
+ }
+ };
+ let mut file = match file {
+ Ok(f) => f,
+ Err(e) => return (Err(e), results),
};
let style = match attrs.formatter {
@@ -357,11 +312,14 @@ where
if tcx.sess.opts.unstable_opts.graphviz_dark_mode {
render_opts.push(dot::RenderOption::DarkTheme);
}
- with_no_trimmed_paths!(dot::render_opts(&graphviz, &mut buf, &render_opts)?);
+ let r = with_no_trimmed_paths!(dot::render_opts(&graphviz, &mut buf, &render_opts));
- file.write_all(&buf)?;
+ let lhs = try {
+ r?;
+ file.write_all(&buf)?;
+ };
- Ok(())
+ (lhs, graphviz.into_results())
}
#[derive(Default)]
diff --git a/compiler/rustc_mir_dataflow/src/framework/graphviz.rs b/compiler/rustc_mir_dataflow/src/framework/graphviz.rs
index c12ccba1e..0270e059a 100644
--- a/compiler/rustc_mir_dataflow/src/framework/graphviz.rs
+++ b/compiler/rustc_mir_dataflow/src/framework/graphviz.rs
@@ -12,10 +12,10 @@ use rustc_middle::mir::graphviz_safe_def_name;
use rustc_middle::mir::{self, BasicBlock, Body, Location};
use super::fmt::{DebugDiffWithAdapter, DebugWithAdapter, DebugWithContext};
-use super::{Analysis, CallReturnPlaces, Direction, Results, ResultsRefCursor, ResultsVisitor};
+use super::{Analysis, CallReturnPlaces, Direction, Results, ResultsCursor, ResultsVisitor};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
-pub enum OutputStyle {
+pub(crate) enum OutputStyle {
AfterOnly,
BeforeAndAfter,
}
@@ -29,33 +29,37 @@ impl OutputStyle {
}
}
-pub struct Formatter<'res, 'mir, 'tcx, A>
+pub(crate) struct Formatter<'mir, 'tcx, A>
where
A: Analysis<'tcx>,
{
body: &'mir Body<'tcx>,
- results: RefCell<&'res mut Results<'tcx, A>>,
+ results: RefCell<Option<Results<'tcx, A>>>,
style: OutputStyle,
reachable: BitSet<BasicBlock>,
}
-impl<'res, 'mir, 'tcx, A> Formatter<'res, 'mir, 'tcx, A>
+impl<'mir, 'tcx, A> Formatter<'mir, 'tcx, A>
where
A: Analysis<'tcx>,
{
- pub fn new(
+ pub(crate) fn new(
body: &'mir Body<'tcx>,
- results: &'res mut Results<'tcx, A>,
+ results: Results<'tcx, A>,
style: OutputStyle,
) -> Self {
let reachable = mir::traversal::reachable_as_bitset(body);
- Formatter { body, results: results.into(), style, reachable }
+ Formatter { body, results: Some(results).into(), style, reachable }
+ }
+
+ pub(crate) fn into_results(self) -> Results<'tcx, A> {
+ self.results.into_inner().unwrap()
}
}
/// A pair of a basic block and an index into that basic blocks `successors`.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
-pub struct CfgEdge {
+pub(crate) struct CfgEdge {
source: BasicBlock,
index: usize,
}
@@ -69,7 +73,7 @@ fn dataflow_successors(body: &Body<'_>, bb: BasicBlock) -> Vec<CfgEdge> {
.collect()
}
-impl<'tcx, A> dot::Labeller<'_> for Formatter<'_, '_, 'tcx, A>
+impl<'tcx, A> dot::Labeller<'_> for Formatter<'_, 'tcx, A>
where
A: Analysis<'tcx>,
A::Domain: DebugWithContext<A>,
@@ -88,14 +92,19 @@ where
fn node_label(&self, block: &Self::Node) -> dot::LabelText<'_> {
let mut label = Vec::new();
- let mut results = self.results.borrow_mut();
- let mut fmt = BlockFormatter {
- results: results.as_results_cursor(self.body),
- style: self.style,
- bg: Background::Light,
- };
+ self.results.replace_with(|results| {
+ // `Formatter::result` is a `RefCell<Option<_>>` so we can replace
+ // the value with `None`, move it into the results cursor, move it
+ // back out, and return it to the refcell wrapped in `Some`.
+ let mut fmt = BlockFormatter {
+ results: results.take().unwrap().into_results_cursor(self.body),
+ style: self.style,
+ bg: Background::Light,
+ };
- fmt.write_node_label(&mut label, *block).unwrap();
+ fmt.write_node_label(&mut label, *block).unwrap();
+ Some(fmt.results.into_results())
+ });
dot::LabelText::html(String::from_utf8(label).unwrap())
}
@@ -109,7 +118,7 @@ where
}
}
-impl<'mir, 'tcx, A> dot::GraphWalk<'mir> for Formatter<'_, 'mir, 'tcx, A>
+impl<'mir, 'tcx, A> dot::GraphWalk<'mir> for Formatter<'mir, 'tcx, A>
where
A: Analysis<'tcx>,
{
@@ -143,16 +152,16 @@ where
}
}
-struct BlockFormatter<'res, 'mir, 'tcx, A>
+struct BlockFormatter<'mir, 'tcx, A>
where
A: Analysis<'tcx>,
{
- results: ResultsRefCursor<'res, 'mir, 'tcx, A>,
+ results: ResultsCursor<'mir, 'tcx, A>,
bg: Background,
style: OutputStyle,
}
-impl<'res, 'mir, 'tcx, A> BlockFormatter<'res, 'mir, 'tcx, A>
+impl<'mir, 'tcx, A> BlockFormatter<'mir, 'tcx, A>
where
A: Analysis<'tcx>,
A::Domain: DebugWithContext<A>,
@@ -536,25 +545,13 @@ where
{
type FlowState = A::Domain;
- fn visit_block_start(
- &mut self,
- _results: &mut Results<'tcx, A>,
- state: &Self::FlowState,
- _block_data: &mir::BasicBlockData<'tcx>,
- _block: BasicBlock,
- ) {
+ fn visit_block_start(&mut self, state: &Self::FlowState) {
if A::Direction::IS_FORWARD {
self.prev_state.clone_from(state);
}
}
- fn visit_block_end(
- &mut self,
- _results: &mut Results<'tcx, A>,
- state: &Self::FlowState,
- _block_data: &mir::BasicBlockData<'tcx>,
- _block: BasicBlock,
- ) {
+ fn visit_block_end(&mut self, state: &Self::FlowState) {
if A::Direction::IS_BACKWARD {
self.prev_state.clone_from(state);
}
diff --git a/compiler/rustc_mir_dataflow/src/framework/lattice.rs b/compiler/rustc_mir_dataflow/src/framework/lattice.rs
index 3b89598d2..1c2b475a4 100644
--- a/compiler/rustc_mir_dataflow/src/framework/lattice.rs
+++ b/compiler/rustc_mir_dataflow/src/framework/lattice.rs
@@ -342,7 +342,7 @@ impl<V: Clone> Clone for MaybeReachable<V> {
fn clone_from(&mut self, source: &Self) {
match (&mut *self, source) {
(MaybeReachable::Reachable(x), MaybeReachable::Reachable(y)) => {
- x.clone_from(&y);
+ x.clone_from(y);
}
_ => *self = source.clone(),
}
diff --git a/compiler/rustc_mir_dataflow/src/framework/mod.rs b/compiler/rustc_mir_dataflow/src/framework/mod.rs
index 5020a1cf0..09cdb055a 100644
--- a/compiler/rustc_mir_dataflow/src/framework/mod.rs
+++ b/compiler/rustc_mir_dataflow/src/framework/mod.rs
@@ -45,9 +45,9 @@ pub mod graphviz;
pub mod lattice;
mod visitor;
-pub use self::cursor::{AnalysisResults, ResultsClonedCursor, ResultsCursor, ResultsRefCursor};
+pub use self::cursor::ResultsCursor;
pub use self::direction::{Backward, Direction, Forward};
-pub use self::engine::{Engine, EntrySets, Results, ResultsCloned};
+pub use self::engine::{Engine, Results};
pub use self::lattice::{JoinSemiLattice, MaybeReachable};
pub use self::visitor::{visit_results, ResultsVisitable, ResultsVisitor};
@@ -246,35 +246,21 @@ pub trait Analysis<'tcx>: AnalysisDomain<'tcx> {
}
}
-/// Defines an `Analysis` which can be cloned for use in multiple `ResultsCursor`s or
-/// `ResultsVisitor`s. Note this need not be a full clone, only enough of one to be used with a new
-/// `ResultsCursor` or `ResultsVisitor`
-pub trait CloneAnalysis {
- fn clone_analysis(&self) -> Self;
-}
-impl<'tcx, A> CloneAnalysis for A
-where
- A: Analysis<'tcx> + Copy,
-{
- fn clone_analysis(&self) -> Self {
- *self
- }
-}
-
/// A gen/kill dataflow problem.
///
-/// Each method in this trait has a corresponding one in `Analysis`. However, these methods only
-/// allow modification of the dataflow state via "gen" and "kill" operations. By defining transfer
-/// functions for each statement in this way, the transfer function for an entire basic block can
-/// be computed efficiently.
+/// Each method in this trait has a corresponding one in `Analysis`. However, the first two methods
+/// here only allow modification of the dataflow state via "gen" and "kill" operations. By defining
+/// transfer functions for each statement in this way, the transfer function for an entire basic
+/// block can be computed efficiently. The remaining methods match up with `Analysis` exactly.
///
-/// `Analysis` is automatically implemented for all implementers of `GenKillAnalysis`.
+/// `Analysis` is automatically implemented for all implementers of `GenKillAnalysis` via a blanket
+/// impl below.
pub trait GenKillAnalysis<'tcx>: Analysis<'tcx> {
type Idx: Idx;
fn domain_size(&self, body: &mir::Body<'tcx>) -> usize;
- /// See `Analysis::apply_statement_effect`.
+ /// See `Analysis::apply_statement_effect`. Note how the second arg differs.
fn statement_effect(
&mut self,
trans: &mut impl GenKill<Self::Idx>,
@@ -282,7 +268,8 @@ pub trait GenKillAnalysis<'tcx>: Analysis<'tcx> {
location: Location,
);
- /// See `Analysis::apply_before_statement_effect`.
+ /// See `Analysis::apply_before_statement_effect`. Note how the second arg
+ /// differs.
fn before_statement_effect(
&mut self,
_trans: &mut impl GenKill<Self::Idx>,
@@ -302,7 +289,7 @@ pub trait GenKillAnalysis<'tcx>: Analysis<'tcx> {
/// See `Analysis::apply_before_terminator_effect`.
fn before_terminator_effect(
&mut self,
- _trans: &mut impl GenKill<Self::Idx>,
+ _trans: &mut Self::Domain,
_terminator: &mir::Terminator<'tcx>,
_location: Location,
) {
@@ -313,7 +300,7 @@ pub trait GenKillAnalysis<'tcx>: Analysis<'tcx> {
/// See `Analysis::apply_call_return_effect`.
fn call_return_effect(
&mut self,
- trans: &mut impl GenKill<Self::Idx>,
+ trans: &mut Self::Domain,
block: BasicBlock,
return_places: CallReturnPlaces<'_, 'tcx>,
);
@@ -328,6 +315,7 @@ pub trait GenKillAnalysis<'tcx>: Analysis<'tcx> {
}
}
+// Blanket impl: any impl of `GenKillAnalysis` automatically impls `Analysis`.
impl<'tcx, A> Analysis<'tcx> for A
where
A: GenKillAnalysis<'tcx>,
diff --git a/compiler/rustc_mir_dataflow/src/framework/tests.rs b/compiler/rustc_mir_dataflow/src/framework/tests.rs
index 9cce5b26c..6b338efd5 100644
--- a/compiler/rustc_mir_dataflow/src/framework/tests.rs
+++ b/compiler/rustc_mir_dataflow/src/framework/tests.rs
@@ -2,9 +2,7 @@
use std::marker::PhantomData;
-use rustc_index::bit_set::BitSet;
use rustc_index::IndexVec;
-use rustc_middle::mir::{self, BasicBlock, Location};
use rustc_middle::ty;
use rustc_span::DUMMY_SP;
@@ -267,8 +265,7 @@ fn test_cursor<D: Direction>(analysis: MockAnalysis<'_, D>) {
let body = analysis.body;
let mut cursor =
- Results { entry_sets: analysis.mock_entry_sets(), analysis, _marker: PhantomData }
- .into_results_cursor(body);
+ Results { entry_sets: analysis.mock_entry_sets(), analysis }.into_results_cursor(body);
cursor.allow_unreachable();
diff --git a/compiler/rustc_mir_dataflow/src/framework/visitor.rs b/compiler/rustc_mir_dataflow/src/framework/visitor.rs
index 3cfa7cc1c..8b8a16bda 100644
--- a/compiler/rustc_mir_dataflow/src/framework/visitor.rs
+++ b/compiler/rustc_mir_dataflow/src/framework/visitor.rs
@@ -1,8 +1,6 @@
-use std::borrow::Borrow;
-
use rustc_middle::mir::{self, BasicBlock, Location};
-use super::{Analysis, Direction, EntrySets, Results};
+use super::{Analysis, Direction, Results};
/// Calls the corresponding method in `ResultsVisitor` for every location in a `mir::Body` with the
/// dataflow state at that location.
@@ -33,14 +31,7 @@ pub fn visit_results<'mir, 'tcx, F, R>(
pub trait ResultsVisitor<'mir, 'tcx, R> {
type FlowState;
- fn visit_block_start(
- &mut self,
- _results: &mut R,
- _state: &Self::FlowState,
- _block_data: &'mir mir::BasicBlockData<'tcx>,
- _block: BasicBlock,
- ) {
- }
+ fn visit_block_start(&mut self, _state: &Self::FlowState) {}
/// Called with the `before_statement_effect` of the given statement applied to `state` but not
/// its `statement_effect`.
@@ -88,20 +79,13 @@ pub trait ResultsVisitor<'mir, 'tcx, R> {
) {
}
- fn visit_block_end(
- &mut self,
- _results: &mut R,
- _state: &Self::FlowState,
- _block_data: &'mir mir::BasicBlockData<'tcx>,
- _block: BasicBlock,
- ) {
- }
+ fn visit_block_end(&mut self, _state: &Self::FlowState) {}
}
/// Things that can be visited by a `ResultsVisitor`.
///
-/// This trait exists so that we can visit the results of multiple dataflow analyses simultaneously.
-/// DO NOT IMPLEMENT MANUALLY. Instead, use the `impl_visitable` macro below.
+/// This trait exists so that we can visit the results of one or more dataflow analyses
+/// simultaneously.
pub trait ResultsVisitable<'tcx> {
type Direction: Direction;
type FlowState;
@@ -143,10 +127,9 @@ pub trait ResultsVisitable<'tcx> {
);
}
-impl<'tcx, A, E> ResultsVisitable<'tcx> for Results<'tcx, A, E>
+impl<'tcx, A> ResultsVisitable<'tcx> for Results<'tcx, A>
where
A: Analysis<'tcx>,
- E: Borrow<EntrySets<'tcx, A>>,
{
type FlowState = A::Domain;