public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/31238]  New: Too many instructions in C loop
@ 2007-03-17 10:15 sigra at home dot se
  2007-04-03  5:23 ` [Bug target/31238] Too many instructions as not reversing loop pinskia at gcc dot gnu dot org
  2007-04-03  6:17 ` steven at gcc dot gnu dot org
  0 siblings, 2 replies; 3+ messages in thread
From: sigra at home dot se @ 2007-03-17 10:15 UTC (permalink / raw)
  To: gcc-bugs

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug target/31238] Too many instructions as not reversing loop
  2007-03-17 10:15 [Bug c/31238] New: Too many instructions in C loop sigra at home dot se
@ 2007-04-03  5:23 ` pinskia at gcc dot gnu dot org
  2007-04-03  6:17 ` steven at gcc dot gnu dot org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2007-04-03  5:23 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2007-04-03 06:23 -------
Most likely what needs to happen is reverse the loop count.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
 GCC target triplet|                            |i?86-*-*
            Summary|Too many instructions in C  |Too many instructions as not
                   |loop                        |reversing loop


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31238


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug target/31238] Too many instructions as not reversing loop
  2007-03-17 10:15 [Bug c/31238] New: Too many instructions in C loop sigra at home dot se
  2007-04-03  5:23 ` [Bug target/31238] Too many instructions as not reversing loop pinskia at gcc dot gnu dot org
@ 2007-04-03  6:17 ` steven at gcc dot gnu dot org
  1 sibling, 0 replies; 3+ messages in thread
From: steven at gcc dot gnu dot org @ 2007-04-03  6:17 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from steven at gcc dot gnu dot org  2007-04-03 07:17 -------
Need to reverse the loop counter here.


-- 

steven at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rakdver at gcc dot gnu dot
                   |                            |org
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
           Keywords|                            |missed-optimization
   Last reconfirmed|0000-00-00 00:00:00         |2007-04-03 07:17:08
               date|                            |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31238


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-04-03  6:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-03-17 10:15 [Bug c/31238] New: Too many instructions in C loop sigra at home dot se
2007-04-03  5:23 ` [Bug target/31238] Too many instructions as not reversing loop pinskia at gcc dot gnu dot org
2007-04-03  6:17 ` steven at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).