1//! A framework that can express both [gen-kill] and generic dataflow problems.
2//!
3//! To use this framework, implement the [`Analysis`] trait. There used to be a `GenKillAnalysis`
4//! alternative trait for gen-kill analyses that would pre-compute the transfer function for each
5//! block. It was intended as an optimization, but it ended up not being any faster than
6//! `Analysis`.
7//!
8//! The `impls` module contains several examples of dataflow analyses.
9//!
10//! Then call `iterate_to_fixpoint` on your type that impls `Analysis` to get a `Results`. From
11//! there, you can use a `ResultsCursor` to inspect the fixpoint solution to your dataflow problem
12//! (good for inspecting a small number of locations), or implement the `ResultsVisitor` interface
13//! and use `visit_results` (good for inspecting many or all locations). The following example uses
14//! the `ResultsCursor` approach.
15//!
16//! ```ignore (cross-crate-imports)
17//! use rustc_const_eval::dataflow::Analysis; // Makes `iterate_to_fixpoint` available.
18//!
19//! fn do_my_analysis(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>) {
20//! let analysis = MyAnalysis::new()
21//! .iterate_to_fixpoint(tcx, body, None)
22//! .into_results_cursor(body);
23//!
24//! // Print the dataflow state *after* each statement in the start block.
25//! for (_, statement_index) in body.block_data[START_BLOCK].statements.iter_enumerated() {
26//! cursor.seek_after(Location { block: START_BLOCK, statement_index });
27//! let state = cursor.get();
28//! println!("{:?}", state);
29//! }
30//! }
31//! ```
32//!
33//! [gen-kill]: https://en.wikipedia.org/wiki/Data-flow_analysis#Bit_vector_problems
3435use std::cmp::Ordering;
3637use rustc_data_structures::work_queue::WorkQueue;
38use rustc_index::bit_set::{DenseBitSet, MixedBitSet};
39use rustc_index::{Idx, IndexVec};
40use rustc_middle::bug;
41use rustc_middle::mir::{
42self, BasicBlock, CallReturnPlaces, Location, SwitchTargetValue, TerminatorEdges, traversal,
43};
44use rustc_middle::ty::TyCtxt;
45use tracing::error;
4647use self::graphviz::write_graphviz_results;
48use super::fmt::DebugWithContext;
4950mod cursor;
51mod direction;
52pub mod fmt;
53pub mod graphviz;
54pub mod lattice;
55mod results;
56mod visitor;
5758pub use self::cursor::ResultsCursor;
59pub use self::direction::{Backward, Direction, Forward};
60pub use self::lattice::{JoinSemiLattice, MaybeReachable};
61pub use self::results::{EntryStates, Results};
62pub use self::visitor::{ResultsVisitor, visit_reachable_results, visit_results};
6364/// Analysis domains are all bitsets of various kinds. This trait holds
65/// operations needed by all of them.
66pub trait BitSetExt<T> {
67fn contains(&self, elem: T) -> bool;
68}
6970impl<T: Idx> BitSetExt<T> for DenseBitSet<T> {
71fn contains(&self, elem: T) -> bool {
72self.contains(elem)
73 }
74}
7576impl<T: Idx> BitSetExt<T> for MixedBitSet<T> {
77fn contains(&self, elem: T) -> bool {
78self.contains(elem)
79 }
80}
8182/// A dataflow problem with an arbitrarily complex transfer function.
83///
84/// This trait specifies the lattice on which this analysis operates (the domain), its
85/// initial value at the entry point of each basic block, and various operations.
86///
87/// # Convergence
88///
89/// When implementing this trait it's possible to choose a transfer function such that the analysis
90/// does not reach fixpoint. To guarantee convergence, your transfer functions must maintain the
91/// following invariant:
92///
93/// > If the dataflow state **before** some point in the program changes to be greater
94/// than the prior state **before** that point, the dataflow state **after** that point must
95/// also change to be greater than the prior state **after** that point.
96///
97/// This invariant guarantees that the dataflow state at a given point in the program increases
98/// monotonically until fixpoint is reached. Note that this monotonicity requirement only applies
99/// to the same point in the program at different points in time. The dataflow state at a given
100/// point in the program may or may not be greater than the state at any preceding point.
101pub trait Analysis<'tcx> {
102/// The type that holds the dataflow state at any given point in the program.
103type Domain: Clone + JoinSemiLattice;
104105/// The direction of this analysis. Either `Forward` or `Backward`.
106type Direction: Direction = Forward;
107108/// Auxiliary data used for analyzing `SwitchInt` terminators, if necessary.
109type SwitchIntData = !;
110111/// A descriptive name for this analysis. Used only for debugging.
112 ///
113 /// This name should be brief and contain no spaces, periods or other characters that are not
114 /// suitable as part of a filename.
115const NAME: &'static str;
116117/// Returns the initial value of the dataflow state upon entry to each basic block.
118fn bottom_value(&self, body: &mir::Body<'tcx>) -> Self::Domain;
119120/// Mutates the initial value of the dataflow state upon entry to the `START_BLOCK`.
121 ///
122 /// For backward analyses, initial state (besides the bottom value) is not yet supported. Trying
123 /// to mutate the initial state will result in a panic.
124//
125 // FIXME: For backward dataflow analyses, the initial state should be applied to every basic
126 // block where control flow could exit the MIR body (e.g., those terminated with `return` or
127 // `resume`). It's not obvious how to handle `yield` points in coroutines, however.
128fn initialize_start_block(&self, body: &mir::Body<'tcx>, state: &mut Self::Domain);
129130/// Updates the current dataflow state with an "early" effect, i.e. one
131 /// that occurs immediately before the given statement.
132 ///
133 /// This method is useful if the consumer of the results of this analysis only needs to observe
134 /// *part* of the effect of a statement (e.g. for two-phase borrows). As a general rule,
135 /// analyses should not implement this without also implementing
136 /// `apply_primary_statement_effect`.
137fn apply_early_statement_effect(
138&self,
139 _state: &mut Self::Domain,
140 _statement: &mir::Statement<'tcx>,
141 _location: Location,
142 ) {
143 }
144145/// Updates the current dataflow state with the effect of evaluating a statement.
146fn apply_primary_statement_effect(
147&self,
148 state: &mut Self::Domain,
149 statement: &mir::Statement<'tcx>,
150 location: Location,
151 );
152153/// Updates the current dataflow state with an effect that occurs immediately *before* the
154 /// given terminator.
155 ///
156 /// This method is useful if the consumer of the results of this analysis needs only to observe
157 /// *part* of the effect of a terminator (e.g. for two-phase borrows). As a general rule,
158 /// analyses should not implement this without also implementing
159 /// `apply_primary_terminator_effect`.
160fn apply_early_terminator_effect(
161&self,
162 _state: &mut Self::Domain,
163 _terminator: &mir::Terminator<'tcx>,
164 _location: Location,
165 ) {
166 }
167168/// Updates the current dataflow state with the effect of evaluating a terminator.
169 ///
170 /// The effect of a successful return from a `Call` terminator should **not** be accounted for
171 /// in this function. That should go in `apply_call_return_effect`. For example, in the
172 /// `InitializedPlaces` analyses, the return place for a function call is not marked as
173 /// initialized here.
174fn apply_primary_terminator_effect<'mir>(
175&self,
176 _state: &mut Self::Domain,
177 terminator: &'mir mir::Terminator<'tcx>,
178 _location: Location,
179 ) -> TerminatorEdges<'mir, 'tcx> {
180terminator.edges()
181 }
182183/* Edge-specific effects */
184185/// Updates the current dataflow state with the effect of a successful return from a `Call`
186 /// terminator.
187 ///
188 /// This is separate from `apply_primary_terminator_effect` to properly track state across
189 /// unwind edges.
190fn apply_call_return_effect(
191&self,
192 _state: &mut Self::Domain,
193 _block: BasicBlock,
194 _return_places: CallReturnPlaces<'_, 'tcx>,
195 ) {
196 }
197198/// Used to update the current dataflow state with the effect of taking a particular branch in
199 /// a `SwitchInt` terminator.
200 ///
201 /// Unlike the other edge-specific effects, which are allowed to mutate `Self::Domain`
202 /// directly, overriders of this method must return a `Self::SwitchIntData` value (wrapped in
203 /// `Some`). The `apply_switch_int_edge_effect` method will then be called once for each
204 /// outgoing edge and will have access to the dataflow state that will be propagated along that
205 /// edge, and also the `Self::SwitchIntData` value.
206 ///
207 /// This interface is somewhat more complex than the other visitor-like "effect" methods.
208 /// However, it is both more ergonomic—callers don't need to recompute or cache information
209 /// about a given `SwitchInt` terminator for each one of its edges—and more efficient—the
210 /// engine doesn't need to clone the exit state for a block unless
211 /// `get_switch_int_data` is actually called.
212fn get_switch_int_data(
213&self,
214 _block: mir::BasicBlock,
215 _discr: &mir::Operand<'tcx>,
216 ) -> Option<Self::SwitchIntData> {
217None218 }
219220/// See comments on `get_switch_int_data`.
221fn apply_switch_int_edge_effect(
222&self,
223 _data: &mut Self::SwitchIntData,
224 _state: &mut Self::Domain,
225 _value: SwitchTargetValue,
226 _targets: &mir::SwitchTargets,
227 ) {
228::core::panicking::panic("internal error: entered unreachable code");unreachable!();
229 }
230231/* Extension methods */
232233/// Finds the fixpoint for this dataflow problem.
234 ///
235 /// You shouldn't need to override this. Its purpose is to enable method chaining like so:
236 ///
237 /// ```ignore (cross-crate-imports)
238 /// let results = MyAnalysis::new(tcx, body)
239 /// .iterate_to_fixpoint(tcx, body, None)
240 /// .into_results_cursor(body);
241 /// ```
242 /// You can optionally add a `pass_name` to the graphviz output for this particular run of a
243 /// dataflow analysis. Some analyses are run multiple times in the compilation pipeline.
244 /// Without a `pass_name` to differentiates them, only the results for the latest run will be
245 /// saved.
246fn iterate_to_fixpoint<'mir>(
247self,
248 tcx: TyCtxt<'tcx>,
249 body: &'mir mir::Body<'tcx>,
250 pass_name: Option<&'static str>,
251 ) -> Results<'tcx, Self>
252where
253Self: Sized,
254Self::Domain: DebugWithContext<Self>,
255 {
256let mut entry_states =
257IndexVec::from_fn_n(|_| self.bottom_value(body), body.basic_blocks.len());
258self.initialize_start_block(body, &mut entry_states[mir::START_BLOCK]);
259260if Self::Direction::IS_BACKWARD && entry_states[mir::START_BLOCK] != self.bottom_value(body)
261 {
262::rustc_middle::util::bug::bug_fmt(format_args!("`initialize_start_block` is not yet supported for backward dataflow analyses"));bug!("`initialize_start_block` is not yet supported for backward dataflow analyses");
263 }
264265let mut dirty_queue: WorkQueue<BasicBlock> = WorkQueue::with_none(body.basic_blocks.len());
266267if Self::Direction::IS_FORWARD {
268for (bb, _) in traversal::reverse_postorder(body) {
269 dirty_queue.insert(bb);
270 }
271 } else {
272// Reverse post-order on the reverse CFG may generate a better iteration order for
273 // backward dataflow analyses, but probably not enough to matter.
274for (bb, _) in traversal::postorder(body) {
275 dirty_queue.insert(bb);
276 }
277 }
278279// `state` is not actually used between iterations;
280 // this is just an optimization to avoid reallocating
281 // every iteration.
282let mut state = self.bottom_value(body);
283while let Some(bb) = dirty_queue.pop() {
284// Set the state to the entry state of the block. This is equivalent to `state =
285 // entry_states[bb].clone()`, but it saves an allocation, thus improving compile times.
286state.clone_from(&entry_states[bb]);
287288Self::Direction::apply_effects_in_block(
289&self,
290 body,
291&mut state,
292 bb,
293&body[bb],
294 |target: BasicBlock, state: &Self::Domain| {
295let set_changed = entry_states[target].join(state);
296if set_changed {
297 dirty_queue.insert(target);
298 }
299 },
300 );
301 }
302303let results = Results { analysis: self, entry_states };
304305if tcx.sess.opts.unstable_opts.dump_mir_dataflow {
306let res = write_graphviz_results(tcx, body, &results, pass_name);
307if let Err(e) = res {
308{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_dataflow/src/framework/mod.rs:308",
"rustc_mir_dataflow::framework", ::tracing::Level::ERROR,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_dataflow/src/framework/mod.rs"),
::tracing_core::__macro_support::Option::Some(308u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_dataflow::framework"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::ERROR <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::ERROR <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("Failed to write graphviz dataflow results: {0}",
e) as &dyn Value))])
});
} else { ; }
};error!("Failed to write graphviz dataflow results: {}", e);
309 }
310 }
311312results313 }
314}
315316/// The legal operations for a transfer function in a gen/kill problem.
317pub trait GenKill<T> {
318/// Inserts `elem` into the state vector.
319fn gen_(&mut self, elem: T);
320321/// Removes `elem` from the state vector.
322fn kill(&mut self, elem: T);
323324/// Calls `gen` for each element in `elems`.
325fn gen_all(&mut self, elems: impl IntoIterator<Item = T>) {
326for elem in elems {
327self.gen_(elem);
328 }
329 }
330331/// Calls `kill` for each element in `elems`.
332fn kill_all(&mut self, elems: impl IntoIterator<Item = T>) {
333for elem in elems {
334self.kill(elem);
335 }
336 }
337}
338339impl<T: Idx> GenKill<T> for DenseBitSet<T> {
340fn gen_(&mut self, elem: T) {
341self.insert(elem);
342 }
343344fn kill(&mut self, elem: T) {
345self.remove(elem);
346 }
347}
348349impl<T: Idx> GenKill<T> for MixedBitSet<T> {
350fn gen_(&mut self, elem: T) {
351self.insert(elem);
352 }
353354fn kill(&mut self, elem: T) {
355self.remove(elem);
356 }
357}
358359impl<T, S: GenKill<T>> GenKill<T> for MaybeReachable<S> {
360fn gen_(&mut self, elem: T) {
361match self {
362// If the state is not reachable, adding an element does nothing.
363MaybeReachable::Unreachable => {}
364 MaybeReachable::Reachable(set) => set.gen_(elem),
365 }
366 }
367368fn kill(&mut self, elem: T) {
369match self {
370// If the state is not reachable, killing an element does nothing.
371MaybeReachable::Unreachable => {}
372 MaybeReachable::Reachable(set) => set.kill(elem),
373 }
374 }
375}
376377// NOTE: DO NOT CHANGE VARIANT ORDER. The derived `Ord` impls rely on the current order.
378#[derive(#[automatically_derived]
impl ::core::clone::Clone for Effect {
#[inline]
fn clone(&self) -> Effect { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for Effect { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for Effect {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
Effect::Early => "Early",
Effect::Primary => "Primary",
})
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for Effect {
#[inline]
fn eq(&self, other: &Effect) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for Effect {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) -> () {}
}Eq, #[automatically_derived]
impl ::core::cmp::PartialOrd for Effect {
#[inline]
fn partial_cmp(&self, other: &Effect)
-> ::core::option::Option<::core::cmp::Ordering> {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
::core::cmp::PartialOrd::partial_cmp(&__self_discr, &__arg1_discr)
}
}PartialOrd, #[automatically_derived]
impl ::core::cmp::Ord for Effect {
#[inline]
fn cmp(&self, other: &Effect) -> ::core::cmp::Ordering {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
::core::cmp::Ord::cmp(&__self_discr, &__arg1_discr)
}
}Ord)]
379enum Effect {
380/// The "early" effect (e.g., `apply_early_statement_effect`) for a statement/terminator.
381Early,
382383/// The "primary" effect (e.g., `apply_primary_statement_effect`) for a statement/terminator.
384Primary,
385}
386387impl Effect {
388const fn at_index(self, statement_index: usize) -> EffectIndex {
389EffectIndex { effect: self, statement_index }
390 }
391}
392393#[derive(#[automatically_derived]
impl ::core::clone::Clone for EffectIndex {
#[inline]
fn clone(&self) -> EffectIndex {
let _: ::core::clone::AssertParamIsClone<usize>;
let _: ::core::clone::AssertParamIsClone<Effect>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for EffectIndex { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for EffectIndex {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "EffectIndex",
"statement_index", &self.statement_index, "effect", &&self.effect)
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for EffectIndex {
#[inline]
fn eq(&self, other: &EffectIndex) -> bool {
self.statement_index == other.statement_index &&
self.effect == other.effect
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for EffectIndex {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) -> () {
let _: ::core::cmp::AssertParamIsEq<usize>;
let _: ::core::cmp::AssertParamIsEq<Effect>;
}
}Eq)]
394pub struct EffectIndex {
395 statement_index: usize,
396 effect: Effect,
397}
398399impl EffectIndex {
400fn next_in_forward_order(self) -> Self {
401match self.effect {
402 Effect::Early => Effect::Primary.at_index(self.statement_index),
403 Effect::Primary => Effect::Early.at_index(self.statement_index + 1),
404 }
405 }
406407fn next_in_backward_order(self) -> Self {
408match self.effect {
409 Effect::Early => Effect::Primary.at_index(self.statement_index),
410 Effect::Primary => Effect::Early.at_index(self.statement_index - 1),
411 }
412 }
413414/// Returns `true` if the effect at `self` should be applied earlier than the effect at `other`
415 /// in forward order.
416fn precedes_in_forward_order(self, other: Self) -> bool {
417let ord = self418 .statement_index
419 .cmp(&other.statement_index)
420 .then_with(|| self.effect.cmp(&other.effect));
421ord == Ordering::Less422 }
423424/// Returns `true` if the effect at `self` should be applied earlier than the effect at `other`
425 /// in backward order.
426fn precedes_in_backward_order(self, other: Self) -> bool {
427let ord = other428 .statement_index
429 .cmp(&self.statement_index)
430 .then_with(|| self.effect.cmp(&other.effect));
431ord == Ordering::Less432 }
433}
434435#[cfg(test)]
436mod tests;