gcc can optimize this program quite well. However the remaining empty loop is a bit silly. int f() { return 1; } int main() { int a = 0; for (int i = 0; i < 100; ++i) { a += f(); } return a; } > gcc loop.c -O3 -S main: pushl %ebp movl %esp, %ebp subl $8, %esp andl $-16, %esp subl $16, %esp movl $99, %eax .L7: decl %eax jns .L7 movl $100, %eax leave ret
Confirmed but not the documentation says we don't want this but that has changed.
*** Bug 18648 has been marked as a duplicate of this bug. ***
One patch for empty-loop removal was posted here by Zdenek http://gcc.gnu.org/ml/gcc-patches/2004-07/msg01679.html
That patch is just gross. Come on, builtin_maybe_infinite_loop?! There are better and easier ways to kill dead loops. Like, a loop optimizer that simply removes dead loops ;-)
This is a most-wanted feature. Are there hope to see a patch landing for 4.1? Can the patch that Zdenek posted be updated for 4.1? Even if it's gross is surely better than nothing...
I should note that loops with small constants bounds are now removed on the mainline.
Only because they are completely peeled.
Subject: Re: empty loop not removed after optimization On Wednesday 11 May 2005 14:58, giovannibajo at libero dot it wrote: > ------- Additional Comments From giovannibajo at libero dot it 2005-05-11 > 12:58 ------- This is a most-wanted feature. Are there hope to see a patch > landing for 4.1? Can the patch that Zdenek posted be updated for 4.1? Even > if it's gross is surely better than nothing... nothing is better than gross.
I'll hack up something.
Zdenek has a patch: http://gcc.gnu.org/ml/gcc-patches/2005-06/msg02270.html
Fixed in 4.1.0.