This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Alignment of Stack
- To: gcc at gcc dot gnu dot org
- Subject: Alignment of Stack
- From: Frank Klemm <pfk at fuchs dot offl dot uni-jena dot de>
- Date: Fri, 14 Sep 2001 00:51:09 +0200
- >Received: (from pfk@localhost)by fuchs.offl.uni-jena.de (8.9.3/8.9.3/SuSE Linux 8.9.3-0.1) id AAA04468for gcc@gcc.gnu.org; Fri, 14 Sep 2001 00:51:09 +0200
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