Build the following code with "gcc -std=c99 -Wall -Wextra -Werror -Os -S": void q(const unsigned int); void f() {for (unsigned int x = 0; x != 10; ++x) q(77);}
The loop becomes: .L2: subl $12, %esp incl %ebx pushl $77 call q addl $16, %esp cmpl $10, %ebx jne .L2 This is 7 instructions, which is too much. The following equivalent program: void q(const unsigned int); void f() {for (unsigned int x = 10; x; --x) q(77);} becomes: .L2: subl $12, %esp pushl $77 call q addl $16, %esp decl %ebx jne .L2 which is only 6 instructions. Since the programs are equivalent (both just call q(77) 10 times) and the second version becomes shorter than the first, the first version is not optimized properly. The corresponding Ada program with q.ads: procedure Q(N : in Natural); and f.adb: with Q; procedure F is begin for i in 1 .. 10 loop Q(77); end loop; end F; built with "gnatgcc -Os -Wall -Wextra -Wextra -Werror -S f.adb" produces the following loop: .L5: pushl $77 .LCFI3: call _ada_q popl %eax decl %ebx jns .L5 which is only 5 instructions. I know that situations are often encountered where C code can not be optimized as much as Ada code, because it would break some bizarre C feature. I do not know it this is such a situation, or if the C code could actually become as tight as the Ada code when compiled and optimized. But at least the first version of the C code should be optimized to be as tight as the second version. (Tested with gcc 4.1.1 (Gentoo 4.1.1-r3) and gnatgcc 3.4.5 (from Gentoo dev-lang/gnat-3.45).) -- Summary: Too many instructions in C loop Product: gcc Version: 4.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: sigra at home dot se http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31238