public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug other/49095] New: Horrible code generation for trivial decrement with test
@ 2011-05-21  3:46 torvalds@linux-foundation.org
  2011-05-21 10:12 ` [Bug rtl-optimization/49095] " rguenth at gcc dot gnu.org
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: torvalds@linux-foundation.org @ 2011-05-21  3:46 UTC (permalink / raw)
  To: gcc-bugs

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

           Summary: Horrible code generation for trivial decrement with
                    test
           Product: gcc
           Version: 4.5.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: other
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: torvalds@linux-foundation.org


This trivial code:

  extern void fncall(void *);

  int main(int argc, char **argv)
  {
    if (!--*argv)
        fncall(argv);
    return 0;
  }

compiles into this ridiculous x86-64 assembly language:

    movq    (%rsi), %rax
    subq    $1, %rax
    testq    %rax, %rax
    movq    %rax, (%rsi)
    je    .L4

for the "decrement and test result" at -O2. 

I'd have expected that any reasonable compiler would generate something like

    decq    (%rsi)
    je    .L4

instead, which would be smaller and faster (even a "subq $1" would be fine, but
the decq is one byte shorter).

The problem is more noticeable when the memory location is a structure offset,
when the "load+decrement+store" model really results in relatively much bigger
code due to the silly repetition of the memory address, for absolutely no
advantage.

Is there some way that I haven't found to make gcc use the rmw instructions?


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

end of thread, other threads:[~2011-05-29 18:57 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-21  3:46 [Bug other/49095] New: Horrible code generation for trivial decrement with test torvalds@linux-foundation.org
2011-05-21 10:12 ` [Bug rtl-optimization/49095] " rguenth at gcc dot gnu.org
2011-05-21 19:22 ` torvalds@linux-foundation.org
2011-05-21 21:33 ` torvalds@linux-foundation.org
2011-05-27 10:50 ` jakub at gcc dot gnu.org
2011-05-27 12:30 ` jakub at gcc dot gnu.org
2011-05-27 14:22 ` torvalds@linux-foundation.org
2011-05-27 14:55 ` jakub at gcc dot gnu.org
2011-05-27 16:02 ` torvalds@linux-foundation.org
2011-05-27 16:36 ` jakub at gcc dot gnu.org
2011-05-27 16:52 ` torvalds@linux-foundation.org
2011-05-29 18:53 ` jakub at gcc dot gnu.org
2011-05-29 18:57 ` jakub at gcc dot gnu.org
2011-05-29 19:09 ` torvalds@linux-foundation.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).