This is the mail archive of the gcc-bugs@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]

Unoptimal while loops


gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)

The 'while' loop optimization has gone berzerk. The simplest and most
used syntax produces the biggest code. Even with the extra code the
alternatives were smaller.

Example is compiled with -O2 -fomit-frame-pointer -mpentium. Althought
using -O3 or -m386 produced the exact same results.

Also note: when using count-- the gcc produces subl $1,%ecx instead of
the obvious decl %ecx.

Please, tell me that I should only upgrade/downgrade egcs and this
problem is gone.
void foo1(char *data, unsigned count)
{
  while (count--) {
    *data++ *= 2;
  }
}

void foo2(char *data, unsigned count)
{
  if (!count) return;
  do {
    *data++ *= 2;      
  } while (--count);
}

void foo3(char *data, unsigned count)
{
  count++;
  while (--count) {
    *data++ *= 2;
  }
}

void foo4(char *data, unsigned count)
{
  if (!count) return;
  count--;
  do {
    *data++ *= 2;
  } while (count--);
}


egcs-while-loop-code.o:     file format elf32-i386

Disassembly of section .text:

00000000 <foo1>:
foo1():
   0:	8b 54 24 08       	movl   0x8(%esp,1),%edx
   4:	8b 44 24 04       	movl   0x4(%esp,1),%eax
   8:	4a                	decl   %edx
   9:	83 fa ff          	cmpl   $0xffffffff,%edx
   c:	74 0b             	je     19 <foo1+0x19>
   e:	89 f6             	movl   %esi,%esi
  10:	c0 20 01          	shlb   $0x1,(%eax)
  13:	40                	incl   %eax
  14:	83 ea 01          	subl   $0x1,%edx
  17:	73 f7             	jae    10 <foo1+0x10>
  19:	c3                	ret    
  1a:	89 f6             	movl   %esi,%esi

0000001c <foo2>:
foo2():
  1c:	8b 44 24 04       	movl   0x4(%esp,1),%eax
  20:	8b 54 24 08       	movl   0x8(%esp,1),%edx
  24:	85 d2             	testl  %edx,%edx
  26:	74 07             	je     2f <foo2+0x13>
  28:	c0 20 01          	shlb   $0x1,(%eax)
  2b:	40                	incl   %eax
  2c:	4a                	decl   %edx
  2d:	75 f9             	jne    28 <foo2+0xc>
  2f:	c3                	ret    

00000030 <foo3>:
foo3():
  30:	8b 44 24 04       	movl   0x4(%esp,1),%eax
  34:	8b 54 24 08       	movl   0x8(%esp,1),%edx
  38:	85 d2             	testl  %edx,%edx
  3a:	74 0b             	je     47 <foo3+0x17>
  3c:	8d 74 26 00       	leal   0x0(%esi,1),%esi
  40:	c0 20 01          	shlb   $0x1,(%eax)
  43:	40                	incl   %eax
  44:	4a                	decl   %edx
  45:	75 f9             	jne    40 <foo3+0x10>
  47:	c3                	ret    

00000048 <foo4>:
foo4():
  48:	8b 54 24 04       	movl   0x4(%esp,1),%edx
  4c:	8b 44 24 08       	movl   0x8(%esp,1),%eax
  50:	85 c0             	testl  %eax,%eax
  52:	74 0a             	je     5e <foo4+0x16>
  54:	48                	decl   %eax
  55:	c0 22 01          	shlb   $0x1,(%edx)
  58:	42                	incl   %edx
  59:	83 e8 01          	subl   $0x1,%eax
  5c:	73 f7             	jae    55 <foo4+0xd>
  5e:	c3                	ret    

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