Empty loops where the loop index is an integer are optimized away, but the following loop is not (presumably because it is more difficult to prove that it terminates in finite time?) int main() { for(double i=0; i<1e9; i+=1); } Command line: g++ -O3
Confirmed.
Interesting! What's the difference between 17 and 18? ------------ int main() { double i; for(i=0; i<18; i+=1); /* gcc -O3, empty loop not removed */ } ---------------- int main() { double i; for(i=0; i<17; i+=1); /* gcc -O3, empty loop removed */ }
(In reply to comment #2) > Interesting! What's the difference between 17 and 18? > ------------ > int main() > { > double i; > for(i=0; i<18; i+=1); /* gcc -O3, empty loop not removed */ > } The funny thing occurs in gcc 4, not gcc 6: .file "empty.c" .text .p2align 4,,15 .globl main .type main, @function main: .LFB0: .cfi_startproc xorl %eax, %eax .p2align 4,,10 .p2align 3 .L2: addl $1, %eax cmpl $18, %eax jne .L2 rep ret .cfi_endproc .LFE0: .size main, .-main .ident "GCC: (Ubuntu 4.4.1-4ubuntu9) 4.4.1" .section .note.GNU-stack,"",@progbits
>Interesting! What's the difference between 17 and 18? Exact representation.
(In reply to comment #4) > >Interesting! What's the difference between 17 and 18? > > Exact representation. Complete unrolling.
the 19 case was fixed in GCC 4.5.0 uptill somewhere between 200 and 2000. 2000 and 1e9 is fixed in GCC 10 for C++ by ... and r10-7522-g75efe9cb1f8938 .