Re: gcc inline asm optimizing problem ?

David Wragg (dpw@doc.ic.ac.uk)
02 Dec 1999 23:56:46 +0000


wu_yb <wu_yb@263.net> writes:
> in asm1.c
>
> int i = 1 ;
> __asm__ volatile ( "
> movl %0, %%eax\n
> addl $1, %%eax\n
> movl %%eax, %0\n"
> : :"g"(i) : "eax"
> ) ;
>
> [snip]
>
> How does the gcc optimize programe with "g"(i) ?????

gcc only knows what you tell it in the constraints. You have told gcc
that your assembly has an input, and so it has made %0 something that
is suitable only as a source operand. If you tell it the full story,
that your assembly uses %0 as an input and an output, then it will
know that %0 must be suitable as a source and destination operand. So
changing the constraints to

: "=g"(i) :"0"(i) : "eax"

will fix the problem.

But a better solution would be to use

__asm__ volatile ( "
movl %1, %%eax\n
addl $1, %%eax\n
movl %%eax, %0\n"
: "=g"(i) :"g"(i) : "eax"
) ;

since this gives gcc a bit more freedom to generate better code.

(To followup on Jakub's message, i386.md uses "=g" several times, so I
don't think there is any problem with doing that; gcc understands that
it cannot use an immediate operand in that context.)

David Wragg

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/