On 20 June 2012 14:19, Tobias Pankrath <tobias@pankrath.net> wrote:
It's because the compiler doesn't understand assembly code. It has no
knowledge of what it actually does, and as a result, just treats it as a
black box.

But this is not set in stone. If I teach a compiler how to optimize intrinsics, can't I teach him to understand and optimize a (maybe small) subset of assembler, too? This must happen in the backend anyway, since intrinsics are platform-dependent, no?

It's MUCH easier with intrinsics. Teaching it to understand assembly involves learning a foreign language, and also for the _compiler_ to understand and predict the product of the codegen step. The compiler is usually completely separated from the codegen, it doesn't understand the architecture it targets. But using the knowledge supplied by the intrinsic API, it can do the optimisations it needs to in the usual way.

Declare the intrinsic as pure/nothrow, declare its arguments as in/scope/const/etc, it now knows a lot more about what to expect from the magic code beneath the intrinsic, and it can safely perform regular optimisation around it. Also, the codegen can use standard register assignment, which is important, and integrate nicely with regular program control structure.