Interactions between function inlining and inline assembly

RECOULES Frederic Frederic.RECOULES@cea.fr
Tue Jan 14 11:54:00 GMT 2020


Hello,
I am trying to understand the behaviour of the compiler when it
inlines some parts of code containing inline assembly statements.
I considered the compiler as a black box, but now, I am
wondering if some observed behaviours are made "on purpose" or if there is
room for optimisation?
In other words, does the compiler have special conservative treatments
for inlining function containing inline assembly or is it simply a hard/worthless
problem? 

Let use the following (dumb) snippet as an example:

static int foo (int x)
{
    int r;
    __asm__ ("movl %1, %0"  : "=r" (r) : "m" (x));
    return r;
}

int v;

int main (int argc, char *argv[])
{
    return foo(v);
}

Which when compiled with GCC 9.2 (as long as I know, version does not matter)
-m32 -O3 produce the following code:

main:
  subl $16, %esp
  movl v, %eax
  movl %eax, 12(%esp)
#APP
  movl 12(%esp), %eax
#NO_APP
  addl $16, %esp
  ret


So, even if the function call is inlined, it continues to pass the argument v
by the stack while, in fact, I would have expected it to forward the
address like this:

main:
#APP
  movl v, %eax
#NO_APP
  ret

Note: my point is only valid for "read-only" entries of an inline assembly
statement. If the entry was an "input-output", the compiler has no choice
but to make a copy of the value according to the semantics of C formal
parameters.



More information about the Gcc-help mailing list