Skip to content

Commit

Permalink
Merge pull request #272 from JohnTitor/clarify-aliasing
Browse files Browse the repository at this point in the history
Clarify the conditions on the aliasing section
  • Loading branch information
ehuss authored Jun 18, 2021
2 parents 35cd622 + c996703 commit b44af9d
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/aliasing.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,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

0 comments on commit b44af9d

Please sign in to comment.