https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59371
--- Comment #15 from Steve Ellcey <sje at gcc dot gnu.org> ---
I am not sure yet where and how to improve this automatically but I have found
an interesting hand optimization that could point to a way to fix this. If I
change the original function:
int foo(int *p, unsigned short c)
{
signed short i;
int x = 0;
for (i = 0; i < c; i++) {
x = x + *p; p++;
}
return x;
}
To:
int foo(int *p, unsigned short c)
{
signed short i;
unsigned short new_i;
int x = 0;
if (c > 32767)
for (i = 0; i < c; i++) {
x = x + *p; p++;
}
else
for (new_i = 0; new_i < c; new_i++) {
x = x + *p; p++;
}
return x;
}
Then GCC 5.0 generates an empty infinite loop for the first for loop and
a compact 4 instruction loop (better even then 4.7) for the second for loop.
I am not sure where or if we can do this optimization in GCC but I am going
to investigate some more.