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::{self, BasicBlock, CallReturnPlaces, Location, TerminatorEdges, traversal};
42use rustc_middle::ty::TyCtxt;
43use tracing::error;
4445use self::graphviz::write_graphviz_results;
46use super::fmt::DebugWithContext;
4748mod cursor;
49mod direction;
50pub mod fmt;
51pub mod graphviz;
52pub mod lattice;
53mod results;
54mod visitor;
5556pub use self::cursor::ResultsCursor;
57pub use self::direction::{Backward, Direction, Forward};
58pub use self::lattice::{JoinSemiLattice, MaybeReachable};
59pub use self::results::{EntryStates, Results};
60pub use self::visitor::{ResultsVisitor, visit_reachable_results, visit_results};
6162/// Analysis domains are all bitsets of various kinds. This trait holds
63/// operations needed by all of them.
64pub trait BitSetExt<T> {
65fn contains(&self, elem: T) -> bool;
66}
6768impl<T: Idx> BitSetExt<T> for DenseBitSet<T> {
69fn contains(&self, elem: T) -> bool {
70self.contains(elem)
71 }
72}
7374impl<T: Idx> BitSetExt<T> for MixedBitSet<T> {
75fn contains(&self, elem: T) -> bool {
76self.contains(elem)
77 }
78}
7980/// A dataflow problem with an arbitrarily complex transfer function.
81///
82/// This trait specifies the lattice on which this analysis operates (the domain), its
83/// initial value at the entry point of each basic block, and various operations.
84///
85/// # Convergence
86///
87/// When implementing this trait it's possible to choose a transfer function such that the analysis
88/// does not reach fixpoint. To guarantee convergence, your transfer functions must maintain the
89/// following invariant:
90///
91/// > If the dataflow state **before** some point in the program changes to be greater
92/// than the prior state **before** that point, the dataflow state **after** that point must
93/// also change to be greater than the prior state **after** that point.
94///
95/// This invariant guarantees that the dataflow state at a given point in the program increases
96/// monotonically until fixpoint is reached. Note that this monotonicity requirement only applies
97/// to the same point in the program at different points in time. The dataflow state at a given
98/// point in the program may or may not be greater than the state at any preceding point.
99pub trait Analysis<'tcx> {
100/// The type that holds the dataflow state at any given point in the program.
101type Domain: Clone + JoinSemiLattice;
102103/// The direction of this analysis. Either `Forward` or `Backward`.
104type Direction: Direction = Forward;
105106/// Auxiliary data used for analyzing `SwitchInt` terminators, if necessary.
107type SwitchIntData = !;
108109/// A descriptive name for this analysis. Used only for debugging.
110 ///
111 /// This name should be brief and contain no spaces, periods or other characters that are not
112 /// suitable as part of a filename.
113const NAME: &'static str;
114115/// Returns the initial value of the dataflow state upon entry to each basic block.
116fn bottom_value(&self, body: &mir::Body<'tcx>) -> Self::Domain;
117118/// Mutates the initial value of the dataflow state upon entry to the `START_BLOCK`.
119 ///
120 /// For backward analyses, initial state (besides the bottom value) is not yet supported. Trying
121 /// to mutate the initial state will result in a panic.
122//
123 // FIXME: For backward dataflow analyses, the initial state should be applied to every basic
124 // block where control flow could exit the MIR body (e.g., those terminated with `return` or
125 // `resume`). It's not obvious how to handle `yield` points in coroutines, however.
126fn initialize_start_block(&self, body: &mir::Body<'tcx>, state: &mut Self::Domain);
127128/// Updates the current dataflow state with an "early" effect, i.e. one
129 /// that occurs immediately before the given statement.
130 ///
131 /// This method is useful if the consumer of the results of this analysis only needs to observe
132 /// *part* of the effect of a statement (e.g. for two-phase borrows). As a general rule,
133 /// analyses should not implement this without also implementing
134 /// `apply_primary_statement_effect`.
135fn apply_early_statement_effect(
136&self,
137 _state: &mut Self::Domain,
138 _statement: &mir::Statement<'tcx>,
139 _location: Location,
140 ) {
141 }
142143/// Updates the current dataflow state with the effect of evaluating a statement.
144fn apply_primary_statement_effect(
145&self,
146 state: &mut Self::Domain,
147 statement: &mir::Statement<'tcx>,
148 location: Location,
149 );
150151/// Updates the current dataflow state with an effect that occurs immediately *before* the
152 /// given terminator.
153 ///
154 /// This method is useful if the consumer of the results of this analysis needs only to observe
155 /// *part* of the effect of a terminator (e.g. for two-phase borrows). As a general rule,
156 /// analyses should not implement this without also implementing
157 /// `apply_primary_terminator_effect`.
158fn apply_early_terminator_effect(
159&self,
160 _state: &mut Self::Domain,
161 _terminator: &mir::Terminator<'tcx>,
162 _location: Location,
163 ) {
164 }
165166/// Updates the current dataflow state with the effect of evaluating a terminator.
167 ///
168 /// The effect of a successful return from a `Call` terminator should **not** be accounted for
169 /// in this function. That should go in `apply_call_return_effect`. For example, in the
170 /// `InitializedPlaces` analyses, the return place for a function call is not marked as
171 /// initialized here.
172fn apply_primary_terminator_effect<'mir>(
173&self,
174 _state: &mut Self::Domain,
175 terminator: &'mir mir::Terminator<'tcx>,
176 _location: Location,
177 ) -> TerminatorEdges<'mir, 'tcx> {
178terminator.edges()
179 }
180181/* Edge-specific effects */
182183/// Updates the current dataflow state with the effect of a successful return from a `Call`
184 /// terminator.
185 ///
186 /// This is separate from `apply_primary_terminator_effect` to properly track state across
187 /// unwind edges.
188fn apply_call_return_effect(
189&self,
190 _state: &mut Self::Domain,
191 _block: BasicBlock,
192 _return_places: CallReturnPlaces<'_, 'tcx>,
193 ) {
194 }
195196/// Used to update the current dataflow state with the effect of taking a particular branch in
197 /// a `SwitchInt` terminator.
198 ///
199 /// Unlike the other edge-specific effects, which are allowed to mutate `Self::Domain`
200 /// directly, overriders of this method must return a `Self::SwitchIntData` value (wrapped in
201 /// `Some`). The `apply_switch_int_edge_effect` method will then be called once for each
202 /// outgoing edge and will have access to the dataflow state that will be propagated along that
203 /// edge, and also the `Self::SwitchIntData` value.
204 ///
205 /// This interface is somewhat more complex than the other visitor-like "effect" methods.
206 /// However, it is both more ergonomic—callers don't need to recompute or cache information
207 /// about a given `SwitchInt` terminator for each one of its edges—and more efficient—the
208 /// engine doesn't need to clone the exit state for a block unless
209 /// `get_switch_int_data` is actually called.
210fn get_switch_int_data(
211&self,
212 _block: mir::BasicBlock,
213 _targets: &mir::SwitchTargets,
214 _discr: &mir::Operand<'tcx>,
215 ) -> Option<Self::SwitchIntData> {
216None217 }
218219/// See comments on `get_switch_int_data`.
220fn apply_switch_int_edge_effect(
221&self,
222 _state: &mut Self::Domain,
223 _data: &mut Self::SwitchIntData,
224 _target_idx: SwitchTargetIndex,
225 ) {
226::core::panicking::panic("internal error: entered unreachable code");unreachable!();
227 }
228229/* Extension methods */
230231/// Finds the fixpoint for this dataflow problem.
232 ///
233 /// You shouldn't need to override this. Its purpose is to enable method chaining like so:
234 ///
235 /// ```ignore (cross-crate-imports)
236 /// let results = MyAnalysis::new(tcx, body)
237 /// .iterate_to_fixpoint(tcx, body, None)
238 /// .into_results_cursor(body);
239 /// ```
240 /// You can optionally add a `pass_name` to the graphviz output for this particular run of a
241 /// dataflow analysis. Some analyses are run multiple times in the compilation pipeline.
242 /// Without a `pass_name` to differentiates them, only the results for the latest run will be
243 /// saved.
244fn iterate_to_fixpoint<'mir>(
245self,
246 tcx: TyCtxt<'tcx>,
247 body: &'mir mir::Body<'tcx>,
248 pass_name: Option<&'static str>,
249 ) -> Results<'tcx, Self>
250where
251Self: Sized,
252Self::Domain: DebugWithContext<Self>,
253 {
254let mut entry_states =
255IndexVec::from_fn_n(|_| self.bottom_value(body), body.basic_blocks.len());
256self.initialize_start_block(body, &mut entry_states[mir::START_BLOCK]);
257258if Self::Direction::IS_BACKWARD && entry_states[mir::START_BLOCK] != self.bottom_value(body)
259 {
260::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");
261 }
262263let mut dirty_queue: WorkQueue<BasicBlock> = WorkQueue::with_none(body.basic_blocks.len());
264265if Self::Direction::IS_FORWARD {
266for (bb, _) in traversal::reverse_postorder(body) {
267 dirty_queue.insert(bb);
268 }
269 } else {
270// Reverse post-order on the reverse CFG may generate a better iteration order for
271 // backward dataflow analyses, but probably not enough to matter.
272for (bb, _) in traversal::postorder(body) {
273 dirty_queue.insert(bb);
274 }
275 }
276277// `state` is not actually used between iterations;
278 // this is just an optimization to avoid reallocating
279 // every iteration.
280let mut state = self.bottom_value(body);
281while let Some(bb) = dirty_queue.pop() {
282// Set the state to the entry state of the block. This is equivalent to `state =
283 // entry_states[bb].clone()`, but it saves an allocation, thus improving compile times.
284state.clone_from(&entry_states[bb]);
285286Self::Direction::apply_effects_in_block(
287&self,
288 body,
289&mut state,
290 bb,
291&body[bb],
292 |target: BasicBlock, state: &Self::Domain| {
293let set_changed = entry_states[target].join(state);
294if set_changed {
295 dirty_queue.insert(target);
296 }
297 },
298 );
299 }
300301let results = Results { analysis: self, entry_states };
302303if tcx.sess.opts.unstable_opts.dump_mir_dataflow {
304let res = write_graphviz_results(tcx, body, &results, pass_name);
305if let Err(e) = res {
306{
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:306",
"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(306u32),
::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);
307 }
308 }
309310results311 }
312}
313314#[derive(#[automatically_derived]
impl ::core::fmt::Debug for SwitchTargetIndex {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
SwitchTargetIndex::Normal(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Normal",
&__self_0),
SwitchTargetIndex::Otherwise =>
::core::fmt::Formatter::write_str(f, "Otherwise"),
}
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for SwitchTargetIndex {
#[inline]
fn clone(&self) -> SwitchTargetIndex {
let _: ::core::clone::AssertParamIsClone<usize>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for SwitchTargetIndex { }Copy)]
315pub enum SwitchTargetIndex {
316// Index of a normal switch target.
317Normal(usize),
318// The final "otherwise" fallback target.
319Otherwise,
320}
321322/// The legal operations for a transfer function in a gen/kill problem.
323pub trait GenKill<T> {
324/// Inserts `elem` into the state vector.
325fn gen_(&mut self, elem: T);
326327/// Removes `elem` from the state vector.
328fn kill(&mut self, elem: T);
329330/// Calls `gen` for each element in `elems`.
331fn gen_all(&mut self, elems: impl IntoIterator<Item = T>) {
332for elem in elems {
333self.gen_(elem);
334 }
335 }
336337/// Calls `kill` for each element in `elems`.
338fn kill_all(&mut self, elems: impl IntoIterator<Item = T>) {
339for elem in elems {
340self.kill(elem);
341 }
342 }
343}
344345impl<T: Idx> GenKill<T> for DenseBitSet<T> {
346fn gen_(&mut self, elem: T) {
347self.insert(elem);
348 }
349350fn kill(&mut self, elem: T) {
351self.remove(elem);
352 }
353}
354355impl<T: Idx> GenKill<T> for MixedBitSet<T> {
356fn gen_(&mut self, elem: T) {
357self.insert(elem);
358 }
359360fn kill(&mut self, elem: T) {
361self.remove(elem);
362 }
363}
364365impl<T, S: GenKill<T>> GenKill<T> for MaybeReachable<S> {
366fn gen_(&mut self, elem: T) {
367match self {
368// If the state is not reachable, adding an element does nothing.
369MaybeReachable::Unreachable => {}
370 MaybeReachable::Reachable(set) => set.gen_(elem),
371 }
372 }
373374fn kill(&mut self, elem: T) {
375match self {
376// If the state is not reachable, killing an element does nothing.
377MaybeReachable::Unreachable => {}
378 MaybeReachable::Reachable(set) => set.kill(elem),
379 }
380 }
381}
382383// NOTE: DO NOT CHANGE VARIANT ORDER. The derived `Ord` impls rely on the current order.
384#[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_fields_are_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)]
385enum Effect {
386/// The "early" effect (e.g., `apply_early_statement_effect`) for a statement/terminator.
387Early,
388389/// The "primary" effect (e.g., `apply_primary_statement_effect`) for a statement/terminator.
390Primary,
391}
392393impl Effect {
394const fn at_index(self, statement_index: usize) -> EffectIndex {
395EffectIndex { effect: self, statement_index }
396 }
397}
398399#[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_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<usize>;
let _: ::core::cmp::AssertParamIsEq<Effect>;
}
}Eq)]
400pub struct EffectIndex {
401 statement_index: usize,
402 effect: Effect,
403}
404405impl EffectIndex {
406fn next_in_forward_order(self) -> Self {
407match self.effect {
408 Effect::Early => Effect::Primary.at_index(self.statement_index),
409 Effect::Primary => Effect::Early.at_index(self.statement_index + 1),
410 }
411 }
412413fn next_in_backward_order(self) -> Self {
414match self.effect {
415 Effect::Early => Effect::Primary.at_index(self.statement_index),
416 Effect::Primary => Effect::Early.at_index(self.statement_index - 1),
417 }
418 }
419420/// Returns `true` if the effect at `self` should be applied earlier than the effect at `other`
421 /// in forward order.
422fn precedes_in_forward_order(self, other: Self) -> bool {
423let ord = self424 .statement_index
425 .cmp(&other.statement_index)
426 .then_with(|| self.effect.cmp(&other.effect));
427ord == Ordering::Less428 }
429430/// Returns `true` if the effect at `self` should be applied earlier than the effect at `other`
431 /// in backward order.
432fn precedes_in_backward_order(self, other: Self) -> bool {
433let ord = other434 .statement_index
435 .cmp(&self.statement_index)
436 .then_with(|| self.effect.cmp(&other.effect));
437ord == Ordering::Less438 }
439}
440441#[cfg(test)]
442mod tests;