smaller code for load constant on i386 -Os

John Reiser jreiser@BitWagon.com
Thu Apr 27 09:45:00 GMT 2000


Two bytes can be saved loading a constant in the range -128 to +127
into a register on i386 when using compiler flag -Os [and perhaps -O2].
Instead of "movl $ n,%eax" which requires five bytes of instruction space,
use "pushl $ n; popl %eax" to take advantage of opcode 0x6A, which sign extends
a byte constant, and pushes a 32-bit word onto the stack.
The special case  0==n is already handled as "xorl %eax,%eax".
The special case ~0==n (same as -1==n) can be done via "orl $ ~0,%eax".

Testcase was run on both gcc 2.91.66 (egcs 1.1.2 release), and "gcc 2.96 20000427"
of the nightly builds via the web page http://www.codesourcery.com/gcc-compile.html .

Command line:  gcc -v --save-temps -S -Os -O2 foo.c  > gcc.out 2>&1

-----foo.i pre-processed input
# 1 "foo.c"
int f()
{
	return 6;
}
-----foo.s assembly language output
	.file	"foo.c"
	.version	"01.01"
gcc2_compiled.:
.text
	.align 4
.globl f
	.type	 f,@function
f:
	pushl %ebp
	movl %esp,%ebp
	movl $6,%eax
	leave
	ret
.Lfe1:
	.size	 f,.Lfe1-f
	.ident	"GCC: (GNU) egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)"
-----gcc.out compiler output
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/specs
gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
 /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/cpp -lang-c -v -undef -D__GNUC__=2 -D__GNUC_MINOR__=91 -D__ELF__ -Dunix -Di386 -D__i386__ -Dlinux -D__ELF__ -D__unix__ -D__i386__ -D__i386__ -D__linux__ -D__unix -D__i386 -D__linux -Asystem(posix) -D__OPTIMIZE__ -Asystem(unix) -Acpu(i386) -Amachine(i386) -Di386 -D__i386 -D__i386__ foo.c foo.i
GNU CPP version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release) (i386 Linux/ELF)
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/i386-redhat-linux/include
 /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include
 /usr/include
End of search list.
 /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/cc1 foo.i -quiet -dumpbase foo.c -Os -O2 -version -o foo.s
GNU C version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release) (i386-redhat-linux) compiled by GNU C version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release).
-----end of report

-- 
John Reiser, voice/fax +1 503 297 3754, jreiser@BitWagon.com


More information about the Gcc-bugs mailing list