diff options
| author | Camelid <camelidcamel@gmail.com> | 2020-11-11 12:48:15 -0800 |
|---|---|---|
| committer | Joshua Nelson <joshua@yottadb.com> | 2020-11-12 11:53:00 -0500 |
| commit | 6eafb25a51c8180285f159881238fe8af416bfce (patch) | |
| tree | 02f31a98f7148687768966c08b033fce96b880a1 /src/doc/rustc-dev-guide | |
| parent | 671e815240d74ed869bf512e09cc8cff3e73d82a (diff) | |
| download | rust-6eafb25a51c8180285f159881238fe8af416bfce.tar.gz rust-6eafb25a51c8180285f159881238fe8af416bfce.zip | |
Provide a brief example of a data-flow analysis
Diffstat (limited to 'src/doc/rustc-dev-guide')
| -rw-r--r-- | src/doc/rustc-dev-guide/src/mir/dataflow.md | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/doc/rustc-dev-guide/src/mir/dataflow.md b/src/doc/rustc-dev-guide/src/mir/dataflow.md index 2bf293cc127..b5cbd46868a 100644 --- a/src/doc/rustc-dev-guide/src/mir/dataflow.md +++ b/src/doc/rustc-dev-guide/src/mir/dataflow.md @@ -109,6 +109,32 @@ longer change (the fixpoint will be top). state. Each basic block's entry state is initialized to bottom before the analysis starts. +## A Brief Example + +This section provides a brief example of a simple data-flow analysis at a high +level. It doesn't explain everything you need to know, but hopefully it will +make the rest of this page clearer. + +Let's say we want to do a simple analysis to find if `mem::transmute` may have +been called by a certain point in the program. Our analysis domain will just +be a `bool` that records whether `transmute` has been called so far. The bottom +value will be `false`, since by default `transmute` has not been called. The top +value will be `true`, since our analysis is done as soon as we determine that +`transmute` has been called. Our join operator will just be the boolean OR (`||`) +operator. We use OR and not AND because of this case: + +``` +let x = if some_cond { + std::mem::transmute<i32, u32>(0_i32); // transmute was called! +} else { + 1_u32; // transmute was not called +}; + +// Has transmute been called by this point? We conservatively approximate that +// as yes, and that is why we use the OR operator. +println!("x: {}", x); +``` + ## Inspecting the Results of a Dataflow Analysis Once you have constructed an analysis, you must pass it to an [`Engine`], which |
