public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Useless assembly
@ 2004-11-28 21:40 Sam Lauber
  2004-11-28 21:41 ` Kazu Hirata
                   ` (3 more replies)
  0 siblings, 4 replies; 11+ messages in thread
From: Sam Lauber @ 2004-11-28 21:40 UTC (permalink / raw)
  To: gcc

When I run GCC 3.4.3 on this code:

#include <stdio.h>                                                                                                                             
int main(void)
{
        printf("Hello World!\n");
        return 0;
}
                                                                                                                                                                                                                                                          
it generates the assembly code (this is i686 assembly)

        .file   "test.c"
        .section        .rodata
.LC0:
        .string "Hello World!\n"
        .text
.globl main
        .type   main, @function
main:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        andl    $-16, %esp
        movl    $0, %eax
        addl    $15, %eax
        addl    $15, %eax
        shrl    $4, %eax
        sall    $4, %eax
        subl    %eax, %esp
        movl    $.LC0, (%esp)
        call    printf
        movl    $0, %eax
        leave
        ret
        .size   main, .-main
        .section        .note.GNU-stack,"",@progbits
        .ident  "GCC: (GNU) 3.4.3"
                                                                                                                             
By hand-optimizing the assembly, I made this:

.LC0:
        .string "Hello World!\n"
.globl main
        .type   main, @function
main:
        pushl   %ebp
        movl    %esp, %ebp
        movl    $.LC0, (%esp)
        call    printf
        leave

It worked the exact same. Clearly a lot was unnessecary. Removing "call printf" or "movl $.LC0, (%esp)" caused it not to print "Hello World!". Removing ".globl main" caused the linker to fail with an undefined reference to main. Removing anything else caused it to print Hello World and then generate a segmentation fault. Optimizing it at -O3 (which I'm sure includes SSA, DCE, and CCP) just made the compilation take longer and generate more assembly then the first time. If someone could just automate removing that code in cc1, it would probably mean a lot of optimization. Also, when I just compiled a "return 0;", I would expect that to just generate main and the instruction "ret" (or is it "retn?"). Instead, it generated a lot of extra instructions, then finally "ret". Clearly this is unsatisfactory. It makes cc1, as, and ld slower because they have to compile, assemble, and link more instructions. Each extra instruction makes a compilation time penalty of -3 and a runtime penalty of -1. At least that could be put onto DCE. If extra code gets generated by cc1, as and ld can't optimize it.

Samuel Lauber
-- 
_____________________________________________________________
Web-based SMS services available at http://www.operamail.com.
From your mailbox to local or overseas cell phones.

Powered by Outblaze

^ permalink raw reply	[flat|nested] 11+ messages in thread
* Re: Useless assembly
@ 2004-11-30  3:12 Sam Lauber
  2004-11-30  4:36 ` Robert Dewar
  0 siblings, 1 reply; 11+ messages in thread
From: Sam Lauber @ 2004-11-30  3:12 UTC (permalink / raw)
  To: Robert Dewar, gcc

What's the stack have to do with it? I thought I had a 32-bit i686. Why would we have to align the stack to a 16-bit boundry? And why would there have to be a stack at all?

----- Original Message -----
From: "Robert Dewar" <dewar@gnat.com>
To: "Sam Lauber" <sam124@operamail.com>
Subject: Re: Useless assembly
Date: Sun, 28 Nov 2004 22:26:40 -0500

> 
> Sam Lauber wrote:
> 
> > By hand-optimizing the assembly, I made this:
> > 
> > .LC0:
> >         .string "Hello World!\n"
> > .globl main
> >         .type   main, @function
> > main:
> >         pushl   %ebp
> >         movl    %esp, %ebp
> >         movl    $.LC0, (%esp)
> >         call    printf
> >         leave
> > 
> > It worked the exact same. Clearly a lot was unnessecary. Removing "call printf" or "movl $.LC0, (%esp)" caused it not to print "Hello World!". Removing ".globl main" caused the linker to fail with an undefined reference to main. Removing anything else caused it to print Hello World and then generate a segmentation fault. Optimizing it at -O3 (which I'm sure includes SSA, DCE, and CCP) just made the compilation take longer and generate more assembly then the first time. If someone could just automate removing that code in cc1, it would probably mean a lot of optimization. Also, when I just compiled a "return 0;", I would expect that to just generate main and the instruction "ret" (or is it "retn?"). Instead, it generated a lot of extra instructions, then finally "ret". Clearly this is unsatisfactory. It makes cc1, as, and ld slower because they have to compile, assemble, and link more instructions. Each extra instruction makes a compilation time penalty of -3 and a runtime 
> penalty of -1. At least that could be put onto DCE. If extra code gets generated by cc1, as and ld can't optimize it.
> > 
> > Samuel Lauber
> 
> Yes, but you fail to establish 16-byte alignment for the
> stack, which is the normal default. If you don't want
> 16-byte alignment for the stack, supply an appropriate
> switch (e.g. -mpreferred-stack-boundary=2).
> 
> 

-- 
_____________________________________________________________
Web-based SMS services available at http://www.operamail.com.
From your mailbox to local or overseas cell phones.

Powered by Outblaze

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2004-11-30 15:05 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-11-28 21:40 Useless assembly Sam Lauber
2004-11-28 21:41 ` Kazu Hirata
2004-11-28 21:56   ` Eric Botcazou
2004-11-28 22:06   ` Steven Bosscher
2004-11-28 22:46 ` jlh
2004-11-29  4:25 ` Robert Dewar
2004-11-29 12:04 ` Andreas Schwab
2004-11-30  3:12 Sam Lauber
2004-11-30  4:36 ` Robert Dewar
2004-11-30 14:12   ` Dave Korn
2004-11-30 16:20     ` Robert Dewar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).