Unoptimal while loops

Mikko Tiihonen Mikko.Tiihonen@hut.fi
Fri Apr 16 08:11:00 GMT 1999


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--);
}



More information about the Gcc-bugs mailing list