http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58122
--- Comment #3 from Oleg Endo <olegendo at gcc dot gnu.org> ---
Looking at the tree dump (-fdump-tree-all -fdump-tree-all-details), it seems
that this is related to loop unrolling (dump file *t.cunroll) and induction
variable optimization.
If the number of iterations is small enough the loop gets unrolled and then the
calculations on 'sum' are folded away.
For this one:
int test2 (void)
{
int sum = 0;
for (int x = 0; x < 5; x++)
sum += x;
return sum;
}
t.cunroll shows:
;; Function int test2() (_Z5test2v, funcdef_no=0, decl_uid=1593,
symbol_order=0)
int test2() ()
{
int x;
int sum;
<bb 2>:
return 10;
}
However, with this one:
int test2 (void)
{
int sum = 0;
for (int x = 0; x < 6; x++)
sum += x;
return sum;
}
The unrolled result is:
int test2() ()
{
int x;
int sum;
unsigned int ivtmp_2;
unsigned int ivtmp_14;
unsigned int ivtmp_20;
unsigned int ivtmp_26;
unsigned int ivtmp_32;
unsigned int ivtmp_38;
<bb 2>:
sum_12 = 0;
x_13 = 1;
ivtmp_14 = 5;
sum_18 = sum_12 + x_13;
x_19 = x_13 + 1;
ivtmp_20 = ivtmp_14 + 4294967295;
sum_24 = sum_18 + x_19;
x_25 = x_19 + 1;
ivtmp_26 = ivtmp_20 + 4294967295;
sum_30 = sum_24 + x_25;
x_31 = x_25 + 1;
ivtmp_32 = ivtmp_26 + 4294967295;
sum_36 = sum_30 + x_31;
x_37 = x_31 + 1;
ivtmp_38 = ivtmp_32 + 4294967295;
sum_3 = sum_36 + x_37;
x_4 = x_37 + 1;
ivtmp_2 = ivtmp_38 + 4294967295;
return sum_3;
}