|
1 | 1 | //! A framework that can express both [gen-kill] and generic dataflow problems.
|
2 | 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]. |
| 3 | +//! To actually use this framework, you must implement either the `Analysis` or the |
| 4 | +//! `GenKillAnalysis` trait. If your transfer function can be expressed with only gen/kill |
| 5 | +//! operations, prefer `GenKillAnalysis` since it will run faster while iterating to fixpoint. The |
| 6 | +//! `impls` module contains several examples of gen/kill dataflow analyses. |
5 | 7 | //!
|
6 |
| -//! To actually use this framework, you must implement either the `Analysis` or the `GenKillAnalysis` |
7 |
| -//! trait. If your transfer function can be expressed with only gen/kill operations, prefer |
8 |
| -//! `GenKillAnalysis` since it will run faster while iterating to fixpoint. Create an `Engine` using |
9 |
| -//! the appropriate constructor and call `iterate_to_fixpoint`. You can use a `ResultsCursor` to |
10 |
| -//! inspect the fixpoint solution to your dataflow problem. |
| 8 | +//! Create an `Engine` for your analysis using the `into_engine` method on the `Analysis` trait, |
| 9 | +//! then call `iterate_to_fixpoint`. From there, you can use a `ResultsCursor` to inspect the |
| 10 | +//! fixpoint solution to your dataflow problem, or implement the `ResultsVisitor` interface and use |
| 11 | +//! `visit_results`. The following example uses the `ResultsCursor` approach. |
11 | 12 | //!
|
12 | 13 | //! ```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(); |
| 14 | +//! use rustc_mir::dataflow::Analysis; // Makes `into_engine` available. |
18 | 15 | //!
|
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); |
| 16 | +//! fn do_my_analysis(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>, did: DefId) { |
| 17 | +//! let analysis = MyAnalysis::new() |
| 18 | +//! .into_engine(tcx, body, did) |
| 19 | +//! .iterate_to_fixpoint() |
| 20 | +//! .into_results_cursor(body); |
23 | 21 | //!
|
| 22 | +//! // Print the dataflow state *after* each statement in the start block. |
24 | 23 | //! for (_, statement_index) in body.block_data[START_BLOCK].statements.iter_enumerated() {
|
25 | 24 | //! cursor.seek_after(Location { block: START_BLOCK, statement_index });
|
26 | 25 | //! let state = cursor.get();
|
|
30 | 29 | //! ```
|
31 | 30 | //!
|
32 | 31 | //! [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 |
34 | 32 |
|
35 | 33 | use std::io;
|
36 | 34 |
|
|
0 commit comments