Code size

Frank Klemm pfk@fuchs.offl.uni-jena.de
Sun Sep 9 07:20:00 GMT 2001


long long  x;

void main (void)
{
    x &= 100;
}

------------------------------------------------
Code with -Os (31 bytes + 7.5 bytes for alignment = 38.5 bytes on average)

.align 16
.global main
.type   main,@function

main:
        pushl   %ebp
        xorl    %edx, %edx
        movl    %esp, %ebp
        pushl   %eax
        pushl   %eax
        movl    x, %eax
        andl    $-16, %esp
        movl    %edx, x+4
        andl    $100, %eax
        movl    %eax, x
        leave
        ret
-------------------------------------------------
Code with -Os -fomit-frame-pointer (31 bytes + 7.5 bytes for alignment = 38.5 bytes on average)

.align 16
.global main
.type   main,@function

main:
        pushl   %ebp			<---- incomprehensible
        xorl    %edx, %edx		<---- okay
        movl    %esp, %ebp		<---- incomprehensible
        pushl   %eax			<---- incomprehensible
        pushl   %eax			<---- incomprehensible
        movl    x, %eax			<---- okay
        andl    $-16, %esp		<---- incomprehensible
        movl    %edx, x+4		<---- okay
        andl    $100, %eax		<---- okay
        movl    %eax, x			<---- okay
        leave				<---- incomprehensible
        ret				<---- okay
-------------------------------------------------
Optimized code (14 bytes):

.text
.global main
.type   main,@function
					<---- no .align, we want optimize for size
main:
        xorl    %eax, %eax		<---- xor r,r; mov r,(mem) is shorter than mov $0,(mem)
        movl    $x, %ecx		<---- we have a free reg and x is accessed more than once
        andl    $100, (%ecx)		<---- operation can be done directly in memory
        movl    %eax, 4(%ecx)		<---- wipe memory
        ret

May be the last two statements must be switched to improve speed.

-- 
Frank Klemm



More information about the Gcc mailing list