|
1 |
| -//! A framework for expressing dataflow problems. |
| 1 | +//! A framework that can express both [gen-kill] and generic dataflow problems. |
| 2 | +//! |
| 3 | +//! There is another interface for dataflow in the compiler in `librustc_mir/dataflow/mod.rs`. The |
| 4 | +//! interface in this module will eventually [replace that one][design-meeting]. |
| 5 | +//! |
| 6 | +//! To actually use this framework, you must implement either the `Analysis` or the |
| 7 | +//! `GenKillAnalysis` trait. If your transfer function can be expressed with only gen/kill |
| 8 | +//! operations, prefer `GenKillAnalysis` as it will perform better. Create an `Engine` using the |
| 9 | +//! appropriate constructor and call `iterate_to_fixpoint`. You can use a `ResultsCursor` to |
| 10 | +//! inspect the fixpoint solution to your dataflow problem. |
| 11 | +//! |
| 12 | +//! ```ignore(cross-crate-imports) |
| 13 | +//! fn do_my_analysis(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>, did: DefId) { |
| 14 | +//! let analysis = MyAnalysis::new(); |
| 15 | +//! |
| 16 | +//! // If `MyAnalysis` implements `GenKillAnalysis`. |
| 17 | +//! let results = Engine::new_gen_kill(tcx, body, did, analysis).iterate_to_fixpoint(); |
| 18 | +//! |
| 19 | +//! // If `MyAnalysis` implements `Analysis`. |
| 20 | +//! // let results = Engine::new_generic(tcx, body, did, analysis).iterate_to_fixpoint(); |
| 21 | +//! |
| 22 | +//! let mut cursor = ResultsCursor::new(body, results); |
| 23 | +//! |
| 24 | +//! for (_, statement_index) in body.block_data[START_BLOCK].statements.iter_enumerated() { |
| 25 | +//! cursor.seek_after(Location { block: START_BLOCK, statement_index }); |
| 26 | +//! let state = cursor.get(); |
| 27 | +//! println!("{:?}", state); |
| 28 | +//! } |
| 29 | +//! } |
| 30 | +//! ``` |
| 31 | +//! |
| 32 | +//! [gen-kill]: https://en.wikipedia.org/wiki/Data-flow_analysis#Bit_vector_problems |
| 33 | +//! [design-meeting]https://github.com/rust-lang/compiler-team/issues/202 |
2 | 34 |
|
3 | 35 | use std::io;
|
4 | 36 |
|
@@ -68,7 +100,7 @@ pub trait AnalysisDomain<'tcx>: BottomValue {
|
68 | 100 | }
|
69 | 101 | }
|
70 | 102 |
|
71 |
| -/// Define a dataflow problem with an arbitrarily complex transfer function. |
| 103 | +/// A dataflow problem with an arbitrarily complex transfer function. |
72 | 104 | pub trait Analysis<'tcx>: AnalysisDomain<'tcx> {
|
73 | 105 | /// Updates the current dataflow state with the effect of evaluating a statement.
|
74 | 106 | fn apply_statement_effect(
|
@@ -134,7 +166,7 @@ pub trait Analysis<'tcx>: AnalysisDomain<'tcx> {
|
134 | 166 | );
|
135 | 167 | }
|
136 | 168 |
|
137 |
| -/// Define a gen/kill dataflow problem. |
| 169 | +/// A gen/kill dataflow problem. |
138 | 170 | ///
|
139 | 171 | /// Each method in this trait has a corresponding one in `Analysis`. However, these methods only
|
140 | 172 | /// allow modification of the dataflow state via "gen" and "kill" operations. By defining transfer
|
|
0 commit comments