Merge pull request #272 from JohnTitor/clarify-aliasing

Clarify the conditions on the aliasing section
pull/284/head
Eric Huss 4 years ago committed by GitHub
commit b44af9d964
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -28,6 +28,7 @@ fn compute(input: &u32, output: &mut u32) {
if *input > 5 {
*output *= 2;
}
// remember that `output` will be `2` if `input > 10`
}
```
@ -35,9 +36,12 @@ 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;
}

Loading…
Cancel
Save