At the GoingNative event, during the Interactive Panel on Day2 at the 9 minute mark, Chandler Carruth says:
Pointers create aliasing problems. They slow down your binaries not speed them up.
What does this mean? Can this be illustrated with a (simple) example?
Aliasing affects performance by preventing the compiler from doing certain optimizations. For example:
Looking at this code you might expect that the compiler could load
*valueonce outside the loop and then set every element in the array to that value very quickly. But this isn’t the case due to aliasing. Because*valuecould be an alias for an element of the array it could change on any given iteration. Therefore the code has to load the value every single iteration, resulting in a potentially large slowdown.If the variables could not alias then the above code would be equivalent to the following:
Using LLVM’s online demo to get the generated code, here are the different results:
1) With aliasing
2) Without aliasing
You can see that the version with aliasing has to do more work in the loop body (between the labels
LBB0_2andLBB0_3).