Background
Using gcc 7.2 I found that the compiler output changes when a loop iterates 999 times.
In particular this program (link to compiler explorer using gcc 7.2):
int f()
{
int i=0x7fffffff-998;
while (i+1>i)
i++;
return i;
}
compiles (using -O3 -fwrapv) to:
f():
mov eax, 2147483647
ret
but, if I change the 998 to 999, it instead compiles to:
f():
xor eax, eax
movdqa xmm0, XMMWORD PTR .LC0[rip]
movdqa xmm2, XMMWORD PTR .LC1[rip]
jmp .L2
.L3:
movdqa xmm0, xmm1
.L2:
movdqa xmm1, xmm0
add eax, 1
cmp eax, 250
paddd xmm1, xmm2
jne .L3
pshufd xmm0, xmm0, 255
movd eax, xmm0
ret
.LC0:
.long 2147482648
.long 2147482649
.long 2147482650
.long 2147482651
.LC1:
.long 4
.long 4
.long 4
.long 4
Question
Why does the output change, and is there a switch to control the threshold at which the behaviour changes?
Note
As signed overflow is undefined behaviour, by default the compiler will turn this program into an infinite loop. This is why the -fwrapv option is needed during compilation.