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]

Re: suggestion to improve optimization


Here's another example where register pressure causes gcc to overlook some
simple optimisations.  For the attached program, gcc 2.95.2 generates

foo:
        pushl   %ebp
        movl    %esp, %ebp
        pushl   %edi
        pushl   %esi
        pushl   %ebx
        movl    n, %eax
        subl    $28, %esp
        movl    %eax, -16(%ebp)
        movl    b, %eax
        movl    8(%ebp), %ebx
        movl    %eax, a
        jmp     .L141
        .p2align 2
.L143:
        movl    $.LC0+1, %edx
        cmpl    %edx, %edx
        movl    -16(%ebp), %edi
        je      .L214

...

.L141:
        movb    (%ebx), %al
        testb   %al, %al
        jne     .L143
        movl    -16(%ebp), %eax
        leal    -12(%ebp), %esp
        popl    %ebx
        popl    %esi
        popl    %edi
        popl    %ebp
        ret

Now n could have been moved to %edi in the first place to eliminate the
second reading from -16(%ebp).  This is what happens when some of the code
at the end of the loop is removed which reduces register pressure.
-- 
Debian GNU/Linux 2.2 is out! ( http://www.debian.org/ )
Email:  Herbert Xu ~{PmV>HI~} <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#define _GNU_SOURCE
#include <string.h>

extern char *n;
extern int a, b;

char *foo(const char *s) {
	char *p;

	p = n;
	a = b;

	while (*s) {
		char *q = p;
		size_t len1, len1p, len2, len2p;

		len1 = strcspn(s, "'");
		len2 = strspn(s + len1, "'");

		len1p = len1 ? len1 + 2 : len1;
		switch (len2) {
		case 0:
			len2p = 0;
			break;
		case 1:
			len2p = 2;
			break;
		default:
			len2p = len2 + 2;
		}

		if (len1) {
			*p = '\'';
			q = mempcpy(p + 1, s, len1);
			*q++ = '\'';
			s += len1;
		}

		switch (len2) {
		case 0:
			break;
		case 1:
			*q++ = '\\';
			*q = '\'';
			s++;
			break;
		default:
			*q = '"';
			*(char *) mempcpy(q + 1, s, len2) = '"';
			s += len2;
		}

	}

	return p;
}

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