Kostya
Kostya

Reputation: 1656

Optimizer: optimize inline assembly

I've heard a couple of times that compiler would not optimize inline assembly or that inline assembly is sort of a blackbox for it. I was suspicious and because I haven't seen any cases when the compiler failed, I didn't care.

But today I found a page on GCC wiki titled DontUseInlineAsm. It contained the same issues that people told me before, but there are no details on why compiler wouldn't understand inline asm and therefore wouldn't optimize it. So, does anyone know reasons for compilers to not do these optimizations?

Of course, I'm putting away special cases like

asm volatile("" : : "g"(value) : "memory"); or
asm volatile("" : : : "memory");

when we are explicitly telling the compiler that this code has visible side effects and therefore it shouldn't optimize it away.

Upvotes: 2

Views: 2217

Answers (2)

Ross Ridge
Ross Ridge

Reputation: 39651

Compilers don't optimize inline assembly because that would defeat the purpose. Inline assembly is used when the programmer feels that they know better than the compiler, either the programmer thinks they can generate better code or that the compiler isn't capable of generating the code they want. In the former case the programmer is optimizing the assembly code themselves, if the compiler isn't doing a good enough job of optimizing the equivalent C code then it's not likely it's going to be able to improve the assembly code. In later case there is no equivalent C code, the inline assembly is using instructions or other assembly features that the compiler isn't capable of generating. In that case it's also unlikely it that its going to understand what those instructions actually do in order to optimize the code.

No compiler is capable of translating inline assembly into its internal "byte code" as you suggested in a comment. GCC treats inline assembly as string to paste into it's assembly output. It has absolutely no understanding of the code inside the string. Clang doesn't normally generate assembly as output, so it has a builtin assembler, but it doesn't really understand the assembly code either. It just translates it into machine code which it inserts into the object file output. Microsoft's compiler is another that doesn't normally generate assembly code output, and it actually has an understanding of the assembly, but only to a limited extent. It only understands things like what registers the code uses, so the compiler do things like preserve registers used by the inline assembly. It doesn't know what the assembly code actually does.

If you want the compiler to optimize your code don't use inline assembly. Even if thers isn't a language feature that directly corresponds to the assembly code you want then compiler maybe able to generate it anyways, like Mike Nakis suggested in a comment about ROTL. You can also use intrinsics, functions that extend the language and correspond to various assembly instructions, which compilers are capable of optimizing in many cases.

Upvotes: 4

Mike Nakis
Mike Nakis

Reputation: 62149

Your question appears to be based on the wrong assumption that a compiler first produces assembly, and then, if you want optimized output, then it reads the assembly that it produced, optimizes it, and writes it back. If that was the case, then it should be no big deal to also read and optimize your inline assembly, right?

The compiler does not optimize your inline assembly because the compiler does not optimize any assembly at all, ever. The compiler has no means of understanding assembly at the level required in order to perform optimizations with it. It is none of its business.

The compiler produces optimized machine code by doing special tricks with its internal data structures, (parse trees, intermediate languages like p-code, etc.) which are not assembly.

If an assembly-generation step is involved, it is a write-only step, meaning that the compiler will generate this assembly for you but it will never attempt to read it. That's the job of an assembler. And I never heard of an optimizing assembler.

Therefore, it is pretty safe to assume that no compiler will ever attempt to optimize anyone's inline assembly.

And I do not know about you, but frankly, I would be pretty annoyed if a compiler ever attempted to modify my inline assembly. If I am to use assembly, I will do it precisely because I know (or I think I know) better than the compiler.

Upvotes: 12

Related Questions