4 ms·
The optimizations can easily be gained back by "manual" loading and storing to temporary local variables. The classical: int foo(float *f, int *x) { *x
by brucketeero 2y ago
The optimizations can easily be gained back by "manual" loading and storing to temporary local variables.
The classical:
int foo(float *f, int *x) {
*x = 2;
*f = 3.0f;
return *x; // oh no, without typed based aliasing I have to load x again!
}
Can obviously be rewritten to:
int foo(float *f, int *x) {
int z = 2;
*x = z;
*f = 3.0f;
return z; // ah, thank you programmer, z has not had its address taken, it's obviously 2.
}
- kbolino 2y agoThat code does not violate the aliasing rules in any case. The two functions you wrote are not the same; the first re-reads *x which may return a different value than 2 if *x was modified in between the first and third lines of the function by another thread (or hardware). However, since x is not marked volatile, the compiler will usually optimize the first function to behave the same as the second.