Skip to content

Clarify the conditions on the aliasing section #272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/aliasing.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,20 @@ fn compute(input: &u32, output: &mut u32) {
if *input > 5 {
*output *= 2;
}
// remember that `output` will be `2` if `input > 10`
}
```

We would *like* to be able to optimize it to the following function:

```rust
fn compute(input: &u32, output: &mut u32) {
let cached_input = *input; // keep *input in a register
let cached_input = *input; // keep `*input` in a register
if cached_input > 10 {
*output = 2; // x > 10 implies x > 5, so double and exit immediately
// If the input is greater than 10, the previous code would set the output to 1 and then double it,
// resulting in an output of 2 (because `>10` implies `>5`).
// Here, we avoid the double assignment and just set it directly to 2.
*output = 2;
} else if cached_input > 5 {
*output *= 2;
}
Expand Down