Tuesday, October 22, 2013

The Effects of Compiler Optimizations

A great question on Stack Overflow (via David Smith):

I first noticed in 2009 that gcc (at least on my projects and on my machines) have the tendency to generate noticeably faster code if I optimize for size (-Os) instead of speed (-O2 or -O3) and I have been wondering ever since why.

I have managed to create a (rather silly) code that shows this surprising behavior and is sufficiently small to be posted here.

There’s so much going on in modern processors that it can be tough to predict how code will behave.

1 Comment RSS · Twitter

During the last Euro LLVM conference, a guy from Apple show us a case where optimizing a multiplication and an addition into a single muladd instruction would slow down the code on ARM processor. (slide available here, http://llvm.org/devmtg/2013-04/olesen-slides.pdf but unfortunately no video)

To predict what would be the best optimization, the compiler have to know exactly how the target processor will behave, how many computation units it uses, how it is going to schedule and dispatch instructions on those units, how branch prediction is going to affect loop, and more.

Even having the assembly, this is now very complex to predict what would be faster.

Leave a Comment