Skip to content

Commit 25016ae

Browse files
camelidJoshua Nelson
authored and
Joshua Nelson
committed
Provide a brief example of a data-flow analysis
1 parent 79b3788 commit 25016ae

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/mir/dataflow.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,32 @@ longer change (the fixpoint will be top).
109109
state. Each basic block's entry state is initialized to bottom before the
110110
analysis starts.
111111

112+
## A Brief Example
113+
114+
This section provides a brief example of a simple data-flow analysis at a high
115+
level. It doesn't explain everything you need to know, but hopefully it will
116+
make the rest of this page clearer.
117+
118+
Let's say we want to do a simple analysis to find if `mem::transmute` may have
119+
been called by a certain point in the program. Our analysis domain will just
120+
be a `bool` that records whether `transmute` has been called so far. The bottom
121+
value will be `false`, since by default `transmute` has not been called. The top
122+
value will be `true`, since our analysis is done as soon as we determine that
123+
`transmute` has been called. Our join operator will just be the boolean OR (`||`)
124+
operator. We use OR and not AND because of this case:
125+
126+
```
127+
let x = if some_cond {
128+
std::mem::transmute<i32, u32>(0_i32); // transmute was called!
129+
} else {
130+
1_u32; // transmute was not called
131+
};
132+
133+
// Has transmute been called by this point? We conservatively approximate that
134+
// as yes, and that is why we use the OR operator.
135+
println!("x: {}", x);
136+
```
137+
112138
## Inspecting the Results of a Dataflow Analysis
113139

114140
Once you have constructed an analysis, you must pass it to an [`Engine`], which

0 commit comments

Comments
 (0)