public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Alignment of Stack
@ 2001-09-13 15:53 Frank Klemm
  2001-09-26 16:23 ` Alexandre Oliva
  0 siblings, 1 reply; 2+ messages in thread
From: Frank Klemm @ 2001-09-13 15:53 UTC (permalink / raw)
  To: gcc

Command line ----------------------------------------

gcc302 -O3 -fomit-frame-pointer

C code ----------------------------------------------

unsigned long long x;

void
main ( void )
{
    x >>= 32;
    return 0;
}

Assembler code of gcc ---------------------------------------

main:	pushl	%ebp		<------
	movl	%esp, %ebp	<------
	subl	$8, %esp	<------
	movl	x+4, %eax
	xorl	%edx, %edx
	andl	$-16, %esp	<------
	movl	%edx, x+4
	movl	%eax, x
	movl	%ebp, %esp	<------
	xorl	%eax, %eax
	popl	%ebp		<------
	ret

Optimized Assembler code: ------------------------------

// -O2
main:	movl	x+4, %eax
	xorl	%edx, %edx
	movl	%eax, x
	movl	%edx, x+4
	xorl	%eax, %eax
	ret
	
// -Os	
main:	movl	$x, %ecx
	xorl	%edx, %edx
	movl	4(%ecx), %eax
	movl	%edx, 4(%ecx)
	movl	%eax,  (%ecx)
	xorl	%eax, %eax
	ret

// -Oss
main:	movl	$x+4, %ecx
	xorl	%eax, %eax
	movl	(%ecx), %edx
	movl	%eax, (%ecx)
	movl	%edx, -4(%ecx)
	ret

Current version: 33 byte
proposed -O2:    21 byte (64%)
proposed -Os:    18 byte (55%)
proposed -Oss:   15 byte (45%)


Remarks ----------------------------------------------

Stack alignment of gcc 3.02 is really a nasty thing.
It blows up code, it slows down code. It is really a pain.

Before doing alignment on stack there should be clarified:

  - who is responsable for aligning (caller / called)
  - when it have a chance to speed up code (you need at least 64 bit items)
  - what other conditions are needed to speed up code
    (manipulating %esp decreases speed of push/pop/mov)

Proposal ------------------------------------------------

  - A caller should never align the stack
  - A function aligns the stack if there are arrays of 64 bit local
    variables on the stack
  - If there are many 64 bit local variables on stack the function may align
    the stack
  - functions arguments can be copied to such a local place if the variable
    is often used and not copied to a FPU register.

-- 
Frank Klemm

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

* Re: Alignment of Stack
  2001-09-13 15:53 Alignment of Stack Frank Klemm
@ 2001-09-26 16:23 ` Alexandre Oliva
  0 siblings, 0 replies; 2+ messages in thread
From: Alexandre Oliva @ 2001-09-26 16:23 UTC (permalink / raw)
  To: Frank Klemm; +Cc: gcc

On Sep 13, 2001, Frank Klemm <pfk@fuchs.offl.uni-jena.de> wrote:

> Stack alignment of gcc 3.02 is really a nasty thing.

IIRC, it's fixed in mainline, so that Only main() forces the stack
alignment; other functions just assume it's aligned properly.  main()
is a special function, don't assume what happens in it applies to all
other functions.

-- 
Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer                  aoliva@{cygnus.com, redhat.com}
CS PhD student at IC-Unicamp        oliva@{lsd.ic.unicamp.br, gnu.org}
Free Software Evangelist    *Please* write to mailing lists, not to me

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

end of thread, other threads:[~2001-09-26 16:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-09-13 15:53 Alignment of Stack Frank Klemm
2001-09-26 16:23 ` Alexandre Oliva

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).