summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_mir_dataflow/src/framework
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:57:31 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:57:31 +0000
commitdc0db358abe19481e475e10c32149b53370f1a1c (patch)
treeab8ce99c4b255ce46f99ef402c27916055b899ee /compiler/rustc_mir_dataflow/src/framework
parentReleasing progress-linux version 1.71.1+dfsg1-2~progress7.99u1. (diff)
downloadrustc-dc0db358abe19481e475e10c32149b53370f1a1c.tar.xz
rustc-dc0db358abe19481e475e10c32149b53370f1a1c.zip
Merging upstream version 1.72.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_mir_dataflow/src/framework')
-rw-r--r--compiler/rustc_mir_dataflow/src/framework/cursor.rs112
-rw-r--r--compiler/rustc_mir_dataflow/src/framework/direction.rs70
-rw-r--r--compiler/rustc_mir_dataflow/src/framework/engine.rs114
-rw-r--r--compiler/rustc_mir_dataflow/src/framework/graphviz.rs98
-rw-r--r--compiler/rustc_mir_dataflow/src/framework/mod.rs61
-rw-r--r--compiler/rustc_mir_dataflow/src/framework/tests.rs17
-rw-r--r--compiler/rustc_mir_dataflow/src/framework/visitor.rs45
7 files changed, 344 insertions, 173 deletions
diff --git a/compiler/rustc_mir_dataflow/src/framework/cursor.rs b/compiler/rustc_mir_dataflow/src/framework/cursor.rs
index f3b5544aa..c978bddfe 100644
--- a/compiler/rustc_mir_dataflow/src/framework/cursor.rs
+++ b/compiler/rustc_mir_dataflow/src/framework/cursor.rs
@@ -1,18 +1,60 @@
//! Random access inspection of the results of a dataflow analysis.
-use crate::framework::BitSetExt;
+use crate::{framework::BitSetExt, CloneAnalysis};
-use std::borrow::Borrow;
+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, Results};
+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<'a, 'mir, 'tcx, A> = ResultsCursor<'mir, 'tcx, A, &'a Results<'tcx, A>>;
+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>>;
/// Allows random access inspection of the results of a dataflow analysis.
///
@@ -45,7 +87,38 @@ where
impl<'mir, 'tcx, A, R> ResultsCursor<'mir, 'tcx, A, R>
where
A: Analysis<'tcx>,
- R: Borrow<Results<'tcx, A>>,
+{
+ /// Returns the dataflow state at the current location.
+ pub fn get(&self) -> &A::Domain {
+ &self.state
+ }
+
+ /// Returns the body this analysis was run on.
+ pub fn body(&self) -> &'mir mir::Body<'tcx> {
+ self.body
+ }
+
+ /// Unwraps this cursor, returning the underlying `Results`.
+ pub fn into_results(self) -> R {
+ 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 {
@@ -74,8 +147,13 @@ where
}
/// Returns the underlying `Results`.
- pub fn results(&self) -> &Results<'tcx, A> {
- &self.results.borrow()
+ pub fn results(&mut self) -> &Results<'tcx, A, R::EntrySets> {
+ self.results.borrow()
+ }
+
+ /// Returns the underlying `Results`.
+ pub fn mut_results(&mut self) -> &mut Results<'tcx, A, R::EntrySets> {
+ self.results.borrow_mut()
}
/// Returns the `Analysis` used to generate the underlying `Results`.
@@ -83,9 +161,14 @@ where
&self.results.borrow().analysis
}
- /// Returns the dataflow state at the current location.
- pub fn get(&self) -> &A::Domain {
- &self.state
+ /// 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)
}
/// Resets the cursor to hold the entry set for the given basic block.
@@ -97,7 +180,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.borrow().entry_set_for_block(block));
self.pos = CursorPosition::block_entry(block);
self.state_needs_reset = false;
}
@@ -186,7 +269,7 @@ where
)
};
- let analysis = &self.results.borrow().analysis;
+ let analysis = &mut self.results.borrow_mut().analysis;
let target_effect_index = effect.at_index(target.statement_index);
A::Direction::apply_effects_in_range(
@@ -205,8 +288,8 @@ 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(&A, &mut A::Domain)) {
- f(&self.results.borrow().analysis, &mut self.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);
self.state_needs_reset = true;
}
}
@@ -215,7 +298,6 @@ impl<'mir, 'tcx, A, R> ResultsCursor<'mir, 'tcx, A, R>
where
A: crate::GenKillAnalysis<'tcx>,
A::Domain: BitSetExt<A::Idx>,
- R: Borrow<Results<'tcx, A>>,
{
pub fn contains(&self, elem: A::Idx) -> bool {
self.get().contains(elem)
diff --git a/compiler/rustc_mir_dataflow/src/framework/direction.rs b/compiler/rustc_mir_dataflow/src/framework/direction.rs
index ba328e780..804b44a6b 100644
--- a/compiler/rustc_mir_dataflow/src/framework/direction.rs
+++ b/compiler/rustc_mir_dataflow/src/framework/direction.rs
@@ -16,7 +16,7 @@ pub trait Direction {
///
/// `effects.start()` must precede or equal `effects.end()` in this direction.
fn apply_effects_in_range<'tcx, A>(
- analysis: &A,
+ analysis: &mut A,
state: &mut A::Domain,
block: BasicBlock,
block_data: &mir::BasicBlockData<'tcx>,
@@ -25,7 +25,7 @@ pub trait Direction {
A: Analysis<'tcx>;
fn apply_effects_in_block<'tcx, A>(
- analysis: &A,
+ analysis: &mut A,
state: &mut A::Domain,
block: BasicBlock,
block_data: &mir::BasicBlockData<'tcx>,
@@ -33,7 +33,7 @@ pub trait Direction {
A: Analysis<'tcx>;
fn gen_kill_effects_in_block<'tcx, A>(
- analysis: &A,
+ analysis: &mut A,
trans: &mut GenKillSet<A::Idx>,
block: BasicBlock,
block_data: &mir::BasicBlockData<'tcx>,
@@ -44,13 +44,13 @@ pub trait Direction {
state: &mut F,
block: BasicBlock,
block_data: &'mir mir::BasicBlockData<'tcx>,
- results: &R,
- vis: &mut impl ResultsVisitor<'mir, 'tcx, FlowState = F>,
+ results: &mut R,
+ vis: &mut impl ResultsVisitor<'mir, 'tcx, R, FlowState = F>,
) where
R: ResultsVisitable<'tcx, FlowState = F>;
fn join_state_into_successors_of<'tcx, A>(
- analysis: &A,
+ analysis: &mut A,
tcx: TyCtxt<'tcx>,
body: &mir::Body<'tcx>,
exit_state: &mut A::Domain,
@@ -67,7 +67,7 @@ impl Direction for Backward {
const IS_FORWARD: bool = false;
fn apply_effects_in_block<'tcx, A>(
- analysis: &A,
+ analysis: &mut A,
state: &mut A::Domain,
block: BasicBlock,
block_data: &mir::BasicBlockData<'tcx>,
@@ -87,7 +87,7 @@ impl Direction for Backward {
}
fn gen_kill_effects_in_block<'tcx, A>(
- analysis: &A,
+ analysis: &mut A,
trans: &mut GenKillSet<A::Idx>,
block: BasicBlock,
block_data: &mir::BasicBlockData<'tcx>,
@@ -107,7 +107,7 @@ impl Direction for Backward {
}
fn apply_effects_in_range<'tcx, A>(
- analysis: &A,
+ analysis: &mut A,
state: &mut A::Domain,
block: BasicBlock,
block_data: &mir::BasicBlockData<'tcx>,
@@ -187,36 +187,36 @@ impl Direction for Backward {
state: &mut F,
block: BasicBlock,
block_data: &'mir mir::BasicBlockData<'tcx>,
- results: &R,
- vis: &mut impl ResultsVisitor<'mir, 'tcx, FlowState = F>,
+ results: &mut R,
+ vis: &mut impl ResultsVisitor<'mir, 'tcx, R, FlowState = F>,
) where
R: ResultsVisitable<'tcx, FlowState = F>,
{
results.reset_to_block_entry(state, block);
- vis.visit_block_end(&state, block_data, block);
+ vis.visit_block_end(results, &state, block_data, block);
// Terminator
let loc = Location { block, statement_index: block_data.statements.len() };
let term = block_data.terminator();
results.reconstruct_before_terminator_effect(state, term, loc);
- vis.visit_terminator_before_primary_effect(state, term, loc);
+ vis.visit_terminator_before_primary_effect(results, state, term, loc);
results.reconstruct_terminator_effect(state, term, loc);
- vis.visit_terminator_after_primary_effect(state, term, loc);
+ vis.visit_terminator_after_primary_effect(results, state, term, loc);
for (statement_index, stmt) in block_data.statements.iter().enumerate().rev() {
let loc = Location { block, statement_index };
results.reconstruct_before_statement_effect(state, stmt, loc);
- vis.visit_statement_before_primary_effect(state, stmt, loc);
+ vis.visit_statement_before_primary_effect(results, state, stmt, loc);
results.reconstruct_statement_effect(state, stmt, loc);
- vis.visit_statement_after_primary_effect(state, stmt, loc);
+ vis.visit_statement_after_primary_effect(results, state, stmt, loc);
}
- vis.visit_block_start(state, block_data, block);
+ vis.visit_block_start(results, state, block_data, block);
}
fn join_state_into_successors_of<'tcx, A>(
- analysis: &A,
+ analysis: &mut A,
_tcx: TyCtxt<'tcx>,
body: &mir::Body<'tcx>,
exit_state: &mut A::Domain,
@@ -319,7 +319,7 @@ impl Direction for Forward {
const IS_FORWARD: bool = true;
fn apply_effects_in_block<'tcx, A>(
- analysis: &A,
+ analysis: &mut A,
state: &mut A::Domain,
block: BasicBlock,
block_data: &mir::BasicBlockData<'tcx>,
@@ -339,7 +339,7 @@ impl Direction for Forward {
}
fn gen_kill_effects_in_block<'tcx, A>(
- analysis: &A,
+ analysis: &mut A,
trans: &mut GenKillSet<A::Idx>,
block: BasicBlock,
block_data: &mir::BasicBlockData<'tcx>,
@@ -359,7 +359,7 @@ impl Direction for Forward {
}
fn apply_effects_in_range<'tcx, A>(
- analysis: &A,
+ analysis: &mut A,
state: &mut A::Domain,
block: BasicBlock,
block_data: &mir::BasicBlockData<'tcx>,
@@ -435,35 +435,35 @@ impl Direction for Forward {
state: &mut F,
block: BasicBlock,
block_data: &'mir mir::BasicBlockData<'tcx>,
- results: &R,
- vis: &mut impl ResultsVisitor<'mir, 'tcx, FlowState = F>,
+ results: &mut R,
+ vis: &mut impl ResultsVisitor<'mir, 'tcx, R, FlowState = F>,
) where
R: ResultsVisitable<'tcx, FlowState = F>,
{
results.reset_to_block_entry(state, block);
- vis.visit_block_start(state, block_data, block);
+ vis.visit_block_start(results, state, block_data, block);
for (statement_index, stmt) in block_data.statements.iter().enumerate() {
let loc = Location { block, statement_index };
results.reconstruct_before_statement_effect(state, stmt, loc);
- vis.visit_statement_before_primary_effect(state, stmt, loc);
+ vis.visit_statement_before_primary_effect(results, state, stmt, loc);
results.reconstruct_statement_effect(state, stmt, loc);
- vis.visit_statement_after_primary_effect(state, stmt, loc);
+ vis.visit_statement_after_primary_effect(results, state, stmt, loc);
}
let loc = Location { block, statement_index: block_data.statements.len() };
let term = block_data.terminator();
results.reconstruct_before_terminator_effect(state, term, loc);
- vis.visit_terminator_before_primary_effect(state, term, loc);
+ vis.visit_terminator_before_primary_effect(results, state, term, loc);
results.reconstruct_terminator_effect(state, term, loc);
- vis.visit_terminator_after_primary_effect(state, term, loc);
+ vis.visit_terminator_after_primary_effect(results, state, term, loc);
- vis.visit_block_end(state, block_data, block);
+ vis.visit_block_end(results, state, block_data, block);
}
fn join_state_into_successors_of<'tcx, A>(
- analysis: &A,
+ analysis: &mut A,
_tcx: TyCtxt<'tcx>,
_body: &mir::Body<'tcx>,
exit_state: &mut A::Domain,
@@ -502,15 +502,7 @@ impl Direction for Forward {
propagate(target, exit_state);
}
- Call {
- unwind,
- destination,
- target,
- func: _,
- args: _,
- from_hir_call: _,
- fn_span: _,
- } => {
+ Call { unwind, destination, target, func: _, args: _, call_source: _, fn_span: _ } => {
if let UnwindAction::Cleanup(unwind) = unwind {
propagate(unwind, exit_state);
}
diff --git a/compiler/rustc_mir_dataflow/src/framework/engine.rs b/compiler/rustc_mir_dataflow/src/framework/engine.rs
index 3e8f792e6..c755d7588 100644
--- a/compiler/rustc_mir_dataflow/src/framework/engine.rs
+++ b/compiler/rustc_mir_dataflow/src/framework/engine.rs
@@ -5,7 +5,9 @@ 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;
@@ -22,54 +24,108 @@ use rustc_span::symbol::{sym, Symbol};
use super::fmt::DebugWithContext;
use super::graphviz;
use super::{
- visit_results, Analysis, Direction, GenKill, GenKillAnalysis, GenKillSet, JoinSemiLattice,
- ResultsCursor, ResultsVisitor,
+ visit_results, Analysis, AnalysisDomain, CloneAnalysis, Direction, GenKill, GenKillAnalysis,
+ GenKillSet, JoinSemiLattice, ResultsClonedCursor, ResultsCursor, ResultsRefCursor,
+ 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>
+pub struct Results<'tcx, A, E = EntrySets<'tcx, A>>
where
A: Analysis<'tcx>,
{
pub analysis: A,
- pub(super) entry_sets: IndexVec<BasicBlock, A::Domain>,
+ pub(super) entry_sets: E,
+ pub(super) _marker: PhantomData<&'tcx ()>,
}
-impl<'tcx, A> Results<'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>
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> {
+ ) -> ResultsCursor<'mir, 'tcx, A, Self> {
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[block]
+ &self.entry_sets.borrow()[block]
}
pub fn visit_with<'mir>(
- &self,
+ &mut self,
body: &'mir mir::Body<'tcx>,
blocks: impl IntoIterator<Item = BasicBlock>,
- vis: &mut impl ResultsVisitor<'mir, 'tcx, FlowState = A::Domain>,
+ vis: &mut impl ResultsVisitor<'mir, 'tcx, Self, FlowState = A::Domain>,
) {
visit_results(body, blocks, self, vis)
}
pub fn visit_reachable_with<'mir>(
- &self,
+ &mut self,
body: &'mir mir::Body<'tcx>,
- vis: &mut impl ResultsVisitor<'mir, 'tcx, FlowState = A::Domain>,
+ vis: &mut impl ResultsVisitor<'mir, 'tcx, Self, FlowState = A::Domain>,
) {
let blocks = mir::traversal::reachable(body);
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>
@@ -98,7 +154,7 @@ where
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>, analysis: A) -> Self {
+ pub fn new_gen_kill(tcx: TyCtxt<'tcx>, body: &'a 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).
//
@@ -114,7 +170,7 @@ where
for (block, block_data) in body.basic_blocks.iter_enumerated() {
let trans = &mut trans_for_block[block];
- A::Direction::gen_kill_effects_in_block(&analysis, trans, block, block_data);
+ A::Direction::gen_kill_effects_in_block(&mut analysis, trans, block, block_data);
}
let apply_trans = Box::new(move |bb: BasicBlock, state: &mut A::Domain| {
@@ -171,7 +227,13 @@ where
A::Domain: DebugWithContext<A>,
{
let Engine {
- analysis, body, mut entry_sets, tcx, apply_trans_for_block, pass_name, ..
+ mut analysis,
+ body,
+ mut entry_sets,
+ tcx,
+ apply_trans_for_block,
+ pass_name,
+ ..
} = self;
let mut dirty_queue: WorkQueue<BasicBlock> = WorkQueue::with_none(body.basic_blocks.len());
@@ -203,11 +265,13 @@ where
// Apply the block transfer function, using the cached one if it exists.
match &apply_trans_for_block {
Some(apply) => apply(bb, &mut state),
- None => A::Direction::apply_effects_in_block(&analysis, &mut state, bb, bb_data),
+ None => {
+ A::Direction::apply_effects_in_block(&mut analysis, &mut state, bb, bb_data)
+ }
}
A::Direction::join_state_into_successors_of(
- &analysis,
+ &mut analysis,
tcx,
body,
&mut state,
@@ -221,11 +285,13 @@ where
);
}
- let results = Results { analysis, entry_sets };
+ let mut results = Results { analysis, entry_sets, _marker: PhantomData };
- let res = write_graphviz_results(tcx, &body, &results, pass_name);
- if let Err(e) = res {
- error!("Failed to write graphviz dataflow results: {}", e);
+ if tcx.sess.opts.unstable_opts.dump_mir_dataflow {
+ let res = write_graphviz_results(tcx, &body, &mut results, pass_name);
+ if let Err(e) = res {
+ error!("Failed to write graphviz dataflow results: {}", e);
+ }
}
results
@@ -235,11 +301,11 @@ where
// Graphviz
/// Writes a DOT file containing the results of a dataflow analysis if the user requested it via
-/// `rustc_mir` attributes.
+/// `rustc_mir` attributes and `-Z dump-mir-dataflow`.
fn write_graphviz_results<'tcx, A>(
tcx: TyCtxt<'tcx>,
body: &mir::Body<'tcx>,
- results: &Results<'tcx, A>,
+ results: &mut Results<'tcx, A>,
pass_name: Option<&'static str>,
) -> std::io::Result<()>
where
@@ -264,9 +330,7 @@ where
io::BufWriter::new(fs::File::create(&path)?)
}
- None if tcx.sess.opts.unstable_opts.dump_mir_dataflow
- && dump_enabled(tcx, A::NAME, def_id) =>
- {
+ None if dump_enabled(tcx, A::NAME, def_id) => {
create_dump_file(tcx, ".dot", false, A::NAME, &pass_name.unwrap_or("-----"), body)?
}
diff --git a/compiler/rustc_mir_dataflow/src/framework/graphviz.rs b/compiler/rustc_mir_dataflow/src/framework/graphviz.rs
index 707729f8f..e331533c3 100644
--- a/compiler/rustc_mir_dataflow/src/framework/graphviz.rs
+++ b/compiler/rustc_mir_dataflow/src/framework/graphviz.rs
@@ -1,6 +1,7 @@
//! A helpful diagram for debugging dataflow problems.
use std::borrow::Cow;
+use std::cell::RefCell;
use std::sync::OnceLock;
use std::{io, ops, str};
@@ -28,23 +29,27 @@ impl OutputStyle {
}
}
-pub struct Formatter<'a, 'tcx, A>
+pub struct Formatter<'res, 'mir, 'tcx, A>
where
A: Analysis<'tcx>,
{
- body: &'a Body<'tcx>,
- results: &'a Results<'tcx, A>,
+ body: &'mir Body<'tcx>,
+ results: RefCell<&'res mut Results<'tcx, A>>,
style: OutputStyle,
reachable: BitSet<BasicBlock>,
}
-impl<'a, 'tcx, A> Formatter<'a, 'tcx, A>
+impl<'res, 'mir, 'tcx, A> Formatter<'res, 'mir, 'tcx, A>
where
A: Analysis<'tcx>,
{
- pub fn new(body: &'a Body<'tcx>, results: &'a Results<'tcx, A>, style: OutputStyle) -> Self {
+ pub fn new(
+ body: &'mir Body<'tcx>,
+ results: &'res mut Results<'tcx, A>,
+ style: OutputStyle,
+ ) -> Self {
let reachable = mir::traversal::reachable_as_bitset(body);
- Formatter { body, results, style, reachable }
+ Formatter { body, results: results.into(), style, reachable }
}
}
@@ -64,7 +69,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>,
@@ -83,13 +88,14 @@ 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: ResultsRefCursor::new(self.body, self.results),
+ results: results.as_results_cursor(self.body),
style: self.style,
bg: Background::Light,
};
- fmt.write_node_label(&mut label, self.body, *block).unwrap();
+ fmt.write_node_label(&mut label, *block).unwrap();
dot::LabelText::html(String::from_utf8(label).unwrap())
}
@@ -103,7 +109,7 @@ where
}
}
-impl<'a, 'tcx, A> dot::GraphWalk<'a> for Formatter<'a, 'tcx, A>
+impl<'mir, 'tcx, A> dot::GraphWalk<'mir> for Formatter<'_, 'mir, 'tcx, A>
where
A: Analysis<'tcx>,
{
@@ -137,16 +143,16 @@ where
}
}
-struct BlockFormatter<'a, 'tcx, A>
+struct BlockFormatter<'res, 'mir, 'tcx, A>
where
A: Analysis<'tcx>,
{
- results: ResultsRefCursor<'a, 'a, 'tcx, A>,
+ results: ResultsRefCursor<'res, 'mir, 'tcx, A>,
bg: Background,
style: OutputStyle,
}
-impl<'a, 'tcx, A> BlockFormatter<'a, 'tcx, A>
+impl<'res, 'mir, 'tcx, A> BlockFormatter<'res, 'mir, 'tcx, A>
where
A: Analysis<'tcx>,
A::Domain: DebugWithContext<A>,
@@ -159,12 +165,7 @@ where
bg
}
- fn write_node_label(
- &mut self,
- w: &mut impl io::Write,
- body: &'a Body<'tcx>,
- block: BasicBlock,
- ) -> io::Result<()> {
+ fn write_node_label(&mut self, w: &mut impl io::Write, block: BasicBlock) -> io::Result<()> {
// Sample output:
// +-+-----------------------------------------------+
// A | bb4 |
@@ -215,11 +216,11 @@ where
self.write_row_with_full_state(w, "", "(on start)")?;
// D + E: Statement and terminator transfer functions
- self.write_statements_and_terminator(w, body, block)?;
+ self.write_statements_and_terminator(w, block)?;
// F: State at end of block
- let terminator = body[block].terminator();
+ let terminator = self.results.body()[block].terminator();
// Write the full dataflow state immediately after the terminator if it differs from the
// state at block entry.
@@ -389,10 +390,14 @@ where
fn write_statements_and_terminator(
&mut self,
w: &mut impl io::Write,
- body: &'a Body<'tcx>,
block: BasicBlock,
) -> io::Result<()> {
- let diffs = StateDiffCollector::run(body, block, self.results.results(), self.style);
+ let diffs = StateDiffCollector::run(
+ self.results.body(),
+ block,
+ self.results.mut_results(),
+ self.style,
+ );
let mut diffs_before = diffs.before.map(|v| v.into_iter());
let mut diffs_after = diffs.after.into_iter();
@@ -401,7 +406,7 @@ where
if A::Direction::IS_FORWARD { it.next().unwrap() } else { it.next_back().unwrap() }
};
- for (i, statement) in body[block].statements.iter().enumerate() {
+ for (i, statement) in self.results.body()[block].statements.iter().enumerate() {
let statement_str = format!("{statement:?}");
let index_str = format!("{i}");
@@ -423,7 +428,7 @@ where
assert!(diffs_after.is_empty());
assert!(diffs_before.as_ref().map_or(true, ExactSizeIterator::is_empty));
- let terminator = body[block].terminator();
+ let terminator = self.results.body()[block].terminator();
let mut terminator_str = String::new();
terminator.kind.fmt_head(&mut terminator_str).unwrap();
@@ -492,29 +497,24 @@ where
}
}
-struct StateDiffCollector<'a, 'tcx, A>
-where
- A: Analysis<'tcx>,
-{
- analysis: &'a A,
- prev_state: A::Domain,
+struct StateDiffCollector<D> {
+ prev_state: D,
before: Option<Vec<String>>,
after: Vec<String>,
}
-impl<'a, 'tcx, A> StateDiffCollector<'a, 'tcx, A>
-where
- A: Analysis<'tcx>,
- A::Domain: DebugWithContext<A>,
-{
- fn run(
- body: &'a mir::Body<'tcx>,
+impl<D> StateDiffCollector<D> {
+ fn run<'tcx, A>(
+ body: &mir::Body<'tcx>,
block: BasicBlock,
- results: &'a Results<'tcx, A>,
+ results: &mut Results<'tcx, A>,
style: OutputStyle,
- ) -> Self {
+ ) -> Self
+ where
+ A: Analysis<'tcx, Domain = D>,
+ D: DebugWithContext<A>,
+ {
let mut collector = StateDiffCollector {
- analysis: &results.analysis,
prev_state: results.analysis.bottom_value(body),
after: vec![],
before: (style == OutputStyle::BeforeAndAfter).then_some(vec![]),
@@ -525,7 +525,7 @@ where
}
}
-impl<'a, 'tcx, A> ResultsVisitor<'a, 'tcx> for StateDiffCollector<'a, 'tcx, A>
+impl<'tcx, A> ResultsVisitor<'_, 'tcx, Results<'tcx, A>> for StateDiffCollector<A::Domain>
where
A: Analysis<'tcx>,
A::Domain: DebugWithContext<A>,
@@ -534,6 +534,7 @@ where
fn visit_block_start(
&mut self,
+ _results: &Results<'tcx, A>,
state: &Self::FlowState,
_block_data: &mir::BasicBlockData<'tcx>,
_block: BasicBlock,
@@ -545,6 +546,7 @@ where
fn visit_block_end(
&mut self,
+ _results: &Results<'tcx, A>,
state: &Self::FlowState,
_block_data: &mir::BasicBlockData<'tcx>,
_block: BasicBlock,
@@ -556,45 +558,49 @@ where
fn visit_statement_before_primary_effect(
&mut self,
+ results: &Results<'tcx, A>,
state: &Self::FlowState,
_statement: &mir::Statement<'tcx>,
_location: Location,
) {
if let Some(before) = self.before.as_mut() {
- before.push(diff_pretty(state, &self.prev_state, self.analysis));
+ before.push(diff_pretty(state, &self.prev_state, &results.analysis));
self.prev_state.clone_from(state)
}
}
fn visit_statement_after_primary_effect(
&mut self,
+ results: &Results<'tcx, A>,
state: &Self::FlowState,
_statement: &mir::Statement<'tcx>,
_location: Location,
) {
- self.after.push(diff_pretty(state, &self.prev_state, self.analysis));
+ self.after.push(diff_pretty(state, &self.prev_state, &results.analysis));
self.prev_state.clone_from(state)
}
fn visit_terminator_before_primary_effect(
&mut self,
+ results: &Results<'tcx, A>,
state: &Self::FlowState,
_terminator: &mir::Terminator<'tcx>,
_location: Location,
) {
if let Some(before) = self.before.as_mut() {
- before.push(diff_pretty(state, &self.prev_state, self.analysis));
+ before.push(diff_pretty(state, &self.prev_state, &results.analysis));
self.prev_state.clone_from(state)
}
}
fn visit_terminator_after_primary_effect(
&mut self,
+ results: &Results<'tcx, A>,
state: &Self::FlowState,
_terminator: &mir::Terminator<'tcx>,
_location: Location,
) {
- self.after.push(diff_pretty(state, &self.prev_state, self.analysis));
+ self.after.push(diff_pretty(state, &self.prev_state, &results.analysis));
self.prev_state.clone_from(state)
}
}
diff --git a/compiler/rustc_mir_dataflow/src/framework/mod.rs b/compiler/rustc_mir_dataflow/src/framework/mod.rs
index f2263007f..58df9b9a7 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::{ResultsCursor, ResultsRefCursor};
+pub use self::cursor::{AnalysisResults, ResultsClonedCursor, ResultsCursor, ResultsRefCursor};
pub use self::direction::{Backward, Direction, Forward};
-pub use self::engine::{Engine, Results};
+pub use self::engine::{Engine, EntrySets, Results, ResultsCloned};
pub use self::lattice::{JoinSemiLattice, MeetSemiLattice};
pub use self::visitor::{visit_results, ResultsVisitable, ResultsVisitor};
@@ -146,7 +146,7 @@ pub trait AnalysisDomain<'tcx> {
pub trait Analysis<'tcx>: AnalysisDomain<'tcx> {
/// Updates the current dataflow state with the effect of evaluating a statement.
fn apply_statement_effect(
- &self,
+ &mut self,
state: &mut Self::Domain,
statement: &mir::Statement<'tcx>,
location: Location,
@@ -159,7 +159,7 @@ pub trait Analysis<'tcx>: AnalysisDomain<'tcx> {
/// *part* of the effect of a statement (e.g. for two-phase borrows). As a general rule,
/// analyses should not implement this without also implementing `apply_statement_effect`.
fn apply_before_statement_effect(
- &self,
+ &mut self,
_state: &mut Self::Domain,
_statement: &mir::Statement<'tcx>,
_location: Location,
@@ -173,7 +173,7 @@ pub trait Analysis<'tcx>: AnalysisDomain<'tcx> {
/// `InitializedPlaces` analyses, the return place for a function call is not marked as
/// initialized here.
fn apply_terminator_effect(
- &self,
+ &mut self,
state: &mut Self::Domain,
terminator: &mir::Terminator<'tcx>,
location: Location,
@@ -186,7 +186,7 @@ pub trait Analysis<'tcx>: AnalysisDomain<'tcx> {
/// *part* of the effect of a terminator (e.g. for two-phase borrows). As a general rule,
/// analyses should not implement this without also implementing `apply_terminator_effect`.
fn apply_before_terminator_effect(
- &self,
+ &mut self,
_state: &mut Self::Domain,
_terminator: &mir::Terminator<'tcx>,
_location: Location,
@@ -201,7 +201,7 @@ pub trait Analysis<'tcx>: AnalysisDomain<'tcx> {
/// This is separate from `apply_terminator_effect` to properly track state across unwind
/// edges.
fn apply_call_return_effect(
- &self,
+ &mut self,
state: &mut Self::Domain,
block: BasicBlock,
return_places: CallReturnPlaces<'_, 'tcx>,
@@ -214,7 +214,7 @@ pub trait Analysis<'tcx>: AnalysisDomain<'tcx> {
///
/// By default, no effects happen.
fn apply_yield_resume_effect(
- &self,
+ &mut self,
_state: &mut Self::Domain,
_resume_block: BasicBlock,
_resume_place: mir::Place<'tcx>,
@@ -235,7 +235,7 @@ pub trait Analysis<'tcx>: AnalysisDomain<'tcx> {
/// engine doesn't need to clone the exit state for a block unless
/// `SwitchIntEdgeEffects::apply` is actually called.
fn apply_switch_int_edge_effects(
- &self,
+ &mut self,
_block: BasicBlock,
_discr: &mir::Operand<'tcx>,
_apply_edge_effects: &mut impl SwitchIntEdgeEffects<Self::Domain>,
@@ -269,6 +269,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
@@ -282,7 +297,7 @@ pub trait GenKillAnalysis<'tcx>: Analysis<'tcx> {
/// See `Analysis::apply_statement_effect`.
fn statement_effect(
- &self,
+ &mut self,
trans: &mut impl GenKill<Self::Idx>,
statement: &mir::Statement<'tcx>,
location: Location,
@@ -290,7 +305,7 @@ pub trait GenKillAnalysis<'tcx>: Analysis<'tcx> {
/// See `Analysis::apply_before_statement_effect`.
fn before_statement_effect(
- &self,
+ &mut self,
_trans: &mut impl GenKill<Self::Idx>,
_statement: &mir::Statement<'tcx>,
_location: Location,
@@ -299,7 +314,7 @@ pub trait GenKillAnalysis<'tcx>: Analysis<'tcx> {
/// See `Analysis::apply_terminator_effect`.
fn terminator_effect(
- &self,
+ &mut self,
trans: &mut impl GenKill<Self::Idx>,
terminator: &mir::Terminator<'tcx>,
location: Location,
@@ -307,7 +322,7 @@ pub trait GenKillAnalysis<'tcx>: Analysis<'tcx> {
/// See `Analysis::apply_before_terminator_effect`.
fn before_terminator_effect(
- &self,
+ &mut self,
_trans: &mut impl GenKill<Self::Idx>,
_terminator: &mir::Terminator<'tcx>,
_location: Location,
@@ -318,7 +333,7 @@ pub trait GenKillAnalysis<'tcx>: Analysis<'tcx> {
/// See `Analysis::apply_call_return_effect`.
fn call_return_effect(
- &self,
+ &mut self,
trans: &mut impl GenKill<Self::Idx>,
block: BasicBlock,
return_places: CallReturnPlaces<'_, 'tcx>,
@@ -326,7 +341,7 @@ pub trait GenKillAnalysis<'tcx>: Analysis<'tcx> {
/// See `Analysis::apply_yield_resume_effect`.
fn yield_resume_effect(
- &self,
+ &mut self,
_trans: &mut impl GenKill<Self::Idx>,
_resume_block: BasicBlock,
_resume_place: mir::Place<'tcx>,
@@ -335,7 +350,7 @@ pub trait GenKillAnalysis<'tcx>: Analysis<'tcx> {
/// See `Analysis::apply_switch_int_edge_effects`.
fn switch_int_edge_effects<G: GenKill<Self::Idx>>(
- &self,
+ &mut self,
_block: BasicBlock,
_discr: &mir::Operand<'tcx>,
_edge_effects: &mut impl SwitchIntEdgeEffects<G>,
@@ -349,7 +364,7 @@ where
A::Domain: GenKill<A::Idx> + BitSetExt<A::Idx>,
{
fn apply_statement_effect(
- &self,
+ &mut self,
state: &mut A::Domain,
statement: &mir::Statement<'tcx>,
location: Location,
@@ -358,7 +373,7 @@ where
}
fn apply_before_statement_effect(
- &self,
+ &mut self,
state: &mut A::Domain,
statement: &mir::Statement<'tcx>,
location: Location,
@@ -367,7 +382,7 @@ where
}
fn apply_terminator_effect(
- &self,
+ &mut self,
state: &mut A::Domain,
terminator: &mir::Terminator<'tcx>,
location: Location,
@@ -376,7 +391,7 @@ where
}
fn apply_before_terminator_effect(
- &self,
+ &mut self,
state: &mut A::Domain,
terminator: &mir::Terminator<'tcx>,
location: Location,
@@ -387,7 +402,7 @@ where
/* Edge-specific effects */
fn apply_call_return_effect(
- &self,
+ &mut self,
state: &mut A::Domain,
block: BasicBlock,
return_places: CallReturnPlaces<'_, 'tcx>,
@@ -396,7 +411,7 @@ where
}
fn apply_yield_resume_effect(
- &self,
+ &mut self,
state: &mut A::Domain,
resume_block: BasicBlock,
resume_place: mir::Place<'tcx>,
@@ -405,7 +420,7 @@ where
}
fn apply_switch_int_edge_effects(
- &self,
+ &mut self,
block: BasicBlock,
discr: &mir::Operand<'tcx>,
edge_effects: &mut impl SwitchIntEdgeEffects<A::Domain>,
diff --git a/compiler/rustc_mir_dataflow/src/framework/tests.rs b/compiler/rustc_mir_dataflow/src/framework/tests.rs
index 0fed305b9..cb0ec144e 100644
--- a/compiler/rustc_mir_dataflow/src/framework/tests.rs
+++ b/compiler/rustc_mir_dataflow/src/framework/tests.rs
@@ -40,7 +40,7 @@ fn mock_body<'tcx>() -> mir::Body<'tcx> {
destination: dummy_place.clone(),
target: Some(mir::START_BLOCK),
unwind: mir::UnwindAction::Continue,
- from_hir_call: false,
+ call_source: mir::CallSource::Misc,
fn_span: DUMMY_SP,
},
);
@@ -54,7 +54,7 @@ fn mock_body<'tcx>() -> mir::Body<'tcx> {
destination: dummy_place.clone(),
target: Some(mir::START_BLOCK),
unwind: mir::UnwindAction::Continue,
- from_hir_call: false,
+ call_source: mir::CallSource::Misc,
fn_span: DUMMY_SP,
},
);
@@ -179,7 +179,7 @@ impl<'tcx, D: Direction> AnalysisDomain<'tcx> for MockAnalysis<'tcx, D> {
impl<'tcx, D: Direction> Analysis<'tcx> for MockAnalysis<'tcx, D> {
fn apply_statement_effect(
- &self,
+ &mut self,
state: &mut Self::Domain,
_statement: &mir::Statement<'tcx>,
location: Location,
@@ -189,7 +189,7 @@ impl<'tcx, D: Direction> Analysis<'tcx> for MockAnalysis<'tcx, D> {
}
fn apply_before_statement_effect(
- &self,
+ &mut self,
state: &mut Self::Domain,
_statement: &mir::Statement<'tcx>,
location: Location,
@@ -199,7 +199,7 @@ impl<'tcx, D: Direction> Analysis<'tcx> for MockAnalysis<'tcx, D> {
}
fn apply_terminator_effect(
- &self,
+ &mut self,
state: &mut Self::Domain,
_terminator: &mir::Terminator<'tcx>,
location: Location,
@@ -209,7 +209,7 @@ impl<'tcx, D: Direction> Analysis<'tcx> for MockAnalysis<'tcx, D> {
}
fn apply_before_terminator_effect(
- &self,
+ &mut self,
state: &mut Self::Domain,
_terminator: &mir::Terminator<'tcx>,
location: Location,
@@ -219,7 +219,7 @@ impl<'tcx, D: Direction> Analysis<'tcx> for MockAnalysis<'tcx, D> {
}
fn apply_call_return_effect(
- &self,
+ &mut self,
_state: &mut Self::Domain,
_block: BasicBlock,
_return_places: CallReturnPlaces<'_, 'tcx>,
@@ -266,7 +266,8 @@ fn test_cursor<D: Direction>(analysis: MockAnalysis<'_, D>) {
let body = analysis.body;
let mut cursor =
- Results { entry_sets: analysis.mock_entry_sets(), analysis }.into_results_cursor(body);
+ Results { entry_sets: analysis.mock_entry_sets(), analysis, _marker: PhantomData }
+ .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 75b4e150a..76a729827 100644
--- a/compiler/rustc_mir_dataflow/src/framework/visitor.rs
+++ b/compiler/rustc_mir_dataflow/src/framework/visitor.rs
@@ -1,16 +1,18 @@
+use std::borrow::Borrow;
+
use rustc_middle::mir::{self, BasicBlock, Location};
-use super::{Analysis, Direction, Results};
+use super::{Analysis, Direction, EntrySets, Results};
/// Calls the corresponding method in `ResultsVisitor` for every location in a `mir::Body` with the
/// dataflow state at that location.
-pub fn visit_results<'mir, 'tcx, F, V>(
+pub fn visit_results<'mir, 'tcx, F, R>(
body: &'mir mir::Body<'tcx>,
blocks: impl IntoIterator<Item = BasicBlock>,
- results: &V,
- vis: &mut impl ResultsVisitor<'mir, 'tcx, FlowState = F>,
+ results: &mut R,
+ vis: &mut impl ResultsVisitor<'mir, 'tcx, R, FlowState = F>,
) where
- V: ResultsVisitable<'tcx, FlowState = F>,
+ R: ResultsVisitable<'tcx, FlowState = F>,
{
let mut state = results.new_flow_state(body);
@@ -22,15 +24,18 @@ pub fn visit_results<'mir, 'tcx, F, V>(
assert!(reachable_blocks.contains(block));
let block_data = &body[block];
- V::Direction::visit_results_in_block(&mut state, block, block_data, results, vis);
+ R::Direction::visit_results_in_block(&mut state, block, block_data, results, vis);
}
}
-pub trait ResultsVisitor<'mir, 'tcx> {
+/// A visitor over the results of an `Analysis`. The type parameter `R` is the results type being
+/// visited.
+pub trait ResultsVisitor<'mir, 'tcx, R> {
type FlowState;
fn visit_block_start(
&mut self,
+ _results: &R,
_state: &Self::FlowState,
_block_data: &'mir mir::BasicBlockData<'tcx>,
_block: BasicBlock,
@@ -41,6 +46,7 @@ pub trait ResultsVisitor<'mir, 'tcx> {
/// its `statement_effect`.
fn visit_statement_before_primary_effect(
&mut self,
+ _results: &R,
_state: &Self::FlowState,
_statement: &'mir mir::Statement<'tcx>,
_location: Location,
@@ -51,6 +57,7 @@ pub trait ResultsVisitor<'mir, 'tcx> {
/// statement applied to `state`.
fn visit_statement_after_primary_effect(
&mut self,
+ _results: &R,
_state: &Self::FlowState,
_statement: &'mir mir::Statement<'tcx>,
_location: Location,
@@ -61,6 +68,7 @@ pub trait ResultsVisitor<'mir, 'tcx> {
/// its `terminator_effect`.
fn visit_terminator_before_primary_effect(
&mut self,
+ _results: &R,
_state: &Self::FlowState,
_terminator: &'mir mir::Terminator<'tcx>,
_location: Location,
@@ -73,6 +81,7 @@ pub trait ResultsVisitor<'mir, 'tcx> {
/// The `call_return_effect` (if one exists) will *not* be applied to `state`.
fn visit_terminator_after_primary_effect(
&mut self,
+ _results: &R,
_state: &Self::FlowState,
_terminator: &'mir mir::Terminator<'tcx>,
_location: Location,
@@ -81,6 +90,7 @@ pub trait ResultsVisitor<'mir, 'tcx> {
fn visit_block_end(
&mut self,
+ _results: &R,
_state: &Self::FlowState,
_block_data: &'mir mir::BasicBlockData<'tcx>,
_block: BasicBlock,
@@ -105,37 +115,38 @@ pub trait ResultsVisitable<'tcx> {
fn reset_to_block_entry(&self, state: &mut Self::FlowState, block: BasicBlock);
fn reconstruct_before_statement_effect(
- &self,
+ &mut self,
state: &mut Self::FlowState,
statement: &mir::Statement<'tcx>,
location: Location,
);
fn reconstruct_statement_effect(
- &self,
+ &mut self,
state: &mut Self::FlowState,
statement: &mir::Statement<'tcx>,
location: Location,
);
fn reconstruct_before_terminator_effect(
- &self,
+ &mut self,
state: &mut Self::FlowState,
terminator: &mir::Terminator<'tcx>,
location: Location,
);
fn reconstruct_terminator_effect(
- &self,
+ &mut self,
state: &mut Self::FlowState,
terminator: &mir::Terminator<'tcx>,
location: Location,
);
}
-impl<'tcx, A> ResultsVisitable<'tcx> for Results<'tcx, A>
+impl<'tcx, A, E> ResultsVisitable<'tcx> for Results<'tcx, A, E>
where
A: Analysis<'tcx>,
+ E: Borrow<EntrySets<'tcx, A>>,
{
type FlowState = A::Domain;
@@ -146,11 +157,11 @@ where
}
fn reset_to_block_entry(&self, state: &mut Self::FlowState, block: BasicBlock) {
- state.clone_from(&self.entry_set_for_block(block));
+ state.clone_from(self.entry_set_for_block(block));
}
fn reconstruct_before_statement_effect(
- &self,
+ &mut self,
state: &mut Self::FlowState,
stmt: &mir::Statement<'tcx>,
loc: Location,
@@ -159,7 +170,7 @@ where
}
fn reconstruct_statement_effect(
- &self,
+ &mut self,
state: &mut Self::FlowState,
stmt: &mir::Statement<'tcx>,
loc: Location,
@@ -168,7 +179,7 @@ where
}
fn reconstruct_before_terminator_effect(
- &self,
+ &mut self,
state: &mut Self::FlowState,
term: &mir::Terminator<'tcx>,
loc: Location,
@@ -177,7 +188,7 @@ where
}
fn reconstruct_terminator_effect(
- &self,
+ &mut self,
state: &mut Self::FlowState,
term: &mir::Terminator<'tcx>,
loc: Location,