- To: Daniel Marschall <daniel-marschall@xxxxxxxxxxxxxxx>
- Subject: Re: G++ could optimize ASM code more
- From: Andrew Haley <aph@xxxxxxxxxx>
- Date: Wed, 09 May 2012 08:42:08 +0100
- Cc: "gcc-help@xxxxxxxxxxx" <gcc-help@xxxxxxxxxxx>
- Comment: DKIM? See http://www.dkim.org
- Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys
- In-reply-to: <e8ff8edff20e117587346516e5dd7160@viathinksoft.de>
- User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:11.0) Gecko/20120329 Thunderbird/11.0.1
Hi,
On 05/09/2012 07:29 AM, Daniel Marschall wrote:
> As I was optimizing my program, I found a few things which looked odd
> to me in the assembler code.
>
> I am on an AMD x64_32 box running Debian Squeeze, GCC: (Debian 4.4.5-8)
> 4.4.5.
>
> I am using "-O3 -mtune=native" for optimization.
>
> (2)
>
> Compiling following program:
>
> #include <stdio.h>
> #include <strings.h>
> int main(void) {
> volatile unsigned char a = 4;
> volatile unsigned char b = 6;
> volatile unsigned long long int c = a * b;
> return c;
> }
>
> produces:
>
> .file "main.c"
> .text
> .p2align 4,,15
> .globl main
> .type main, @function
> main:
> .LFB16:
> .cfi_startproc
> .cfi_personality 0x3,__gxx_personality_v0
> movb $4, -1(%rsp)
> movb $6, -2(%rsp)
> movzbl -1(%rsp), %edx
> movzbl -2(%rsp), %eax
> movzbl %dl, %edx
> movzbl %al, %eax
> imull %edx, %eax
> cltq
> movq %rax, -16(%rsp) # REDUNDANT??
> movq -16(%rsp), %rax # REDUNDANT??
> ret
> .cfi_endproc
> .LFE16:
> .size main, .-main
> .ident "GCC: (Debian 4.4.5-8) 4.4.5"
> .section .note.GNU-stack,"",@progbits
>
> AFAIK, the two movq statements are redundant. What do they do? The just
> do rax=rsp[-16] and rsp[-16]=rax . Or am I wrong?
They are the expansion of
c = ...
return c;
The compiler must generate code for these expressions because c is
volatile. The compiler is not allowed to elide any volatile ops.
Andrew.
[Linux C Programming]
[Linux Kernel]
[eCos]
[Fedora Development]
[Fedora Announce]
[Autoconf]
[The DWARVES Debugging Tools]
[Yosemite Campsites]
[Yosemite News]
[Linux GCC]