This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Alignment of Stack


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]