extended asm and input parameters

Andrew Haley aph@redhat.com
Thu Jul 4 10:07:00 GMT 2013


On 07/04/2013 02:43 AM, dw wrote:
> I think I may have answered my own question.  While talking about 
> clobbers, the docs say:
> 
> "You may not write a clobber description in a way that overlaps with an 
> input or output operand."
> "There is no way for you to specify that an input operand is modified 
> without also specifying it as an output operand."
> 
> So (by implication) if you modify an input, you are expected to specify 
> that fact, and you can't do that with a clobber.  So, reading between 
> the lines, the assumption here is that modifying input-only parameters 
> is bad.
> 
> Which brings us to the next question: If input-only parameters are 
> assumed to be unchanged, why are some being re-loaded?  These 
> unnecessary instructions affect both program size and execution performance.
> 
> The code below shows 2 instances of unnecessary assembly code being 
> generated:
> 
> 1) rdi being loaded (unnecessarily) at 407818.
> 2) eax being zeroed twice.
> 
> And I can provide code that shows more.
> 
> I'm trying to decide how important this is.  If this is only happening 
> because I'm using an asm block, that's one thing.  While not good, I'm 
> not optimistic about what's going to happen if I open a bug that only 
> affects performance around inline asm.
> 
> On the other hand, it seems possible that this is a more general problem 
> where the optimizer is simply missing a category of optimization 
> opportunities.  Ok, not likely, but possible.
> 
> To file or not to file.  That is the question.
> 
> If anyone has any thoughts or insight to share, I'd love to hear it.
> 
> dw
> 
> On 7/2/2013 6:41 PM, dw wrote:
>> I'm trying to understand how input parameters are used by gcc's 
>> extended asm.  Each time I think I've got a handle on it, it does 
>> something else unexpected.
>>
>> For example, this c++ code:
>>
>>  inline void moo(unsigned char *Dest, unsigned char Data, int Count) {
>>    asm volatile (
>>       "rep stosb"
>>       : /* no outputs */
>>       : "D" (Dest), "a" (Data), "c" (Count)
>>       : "memory");
>> }
>>
>> int main()
>> {
>>    unsigned char buff1[32];
>>
>>    moo(buff1, 0, sizeof(buff1));
>>    moo(buff1, 0, sizeof(buff1));
>>
>>    return 0;
>> }
>>
>>  Compiling for 64bit on i386 using -Os, I get:

I get:

main:
.LFB1:
	leaq	-32(%rsp), %rdi
	movl	$32, %ecx
	xorl	%eax, %eax
#APP
# 2 "z.c" 1
	rep stosb
# 0 "" 2
# 2 "z.c" 1
	rep stosb
# 0 "" 2
#NO_APP
	xorl	%eax, %eax
	ret

By the way, your asm is wrong: stosb modifies its registers, and you
must tell GCC that.

Andrew.



More information about the Gcc-help mailing list