public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "torvalds@linux-foundation.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug rtl-optimization/49095] Horrible code generation for trivial decrement with test
Date: Sat, 21 May 2011 19:22:00 -0000	[thread overview]
Message-ID: <bug-49095-4-SKRClnYklx@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-49095-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #2 from Linus Torvalds <torvalds@linux-foundation.org> 2011-05-21 18:41:15 UTC ---
(In reply to comment #1)
>
> On the RTL side combine tries to do
> 
> Trying 7, 8 -> 9:
> Failed to match this instruction:
> (parallel [
>         (set (mem/f:DI (reg/v/f:DI 63 [ argv ]) [2 *argv_1(D)+0 S8 A64])
>             (plus:DI (mem/f:DI (reg/v/f:DI 63 [ argv ]) [2 *argv_1(D)+0 S8
> A64])
>                 (const_int -1 [0xffffffffffffffff])))
>         (set (reg/f:DI 60 [ D.2723 ])
>             (plus:DI (mem/f:DI (reg/v/f:DI 63 [ argv ]) [2 *argv_1(D)+0 S8
> A64])
>                 (const_int -1 [0xffffffffffffffff])))
>     ])
> 
> because we have a use of the decrement result in the comparison.  It doesn't
> try to combine this with the comparison though.

Why isn't there a trivial pattern for the combination of "add+cmp0"? It sounds
like a peephole optimization to me.

> So this case is really special ;)  Without the use of the decremented
> value we get the desired subq $1, (%rsi).

The whole notion of "decrement and check if zero" is just about as special as
mud. 

And I realize that without the "check if zero" part I get the single rmw
instruction, but I was really hoping that gcc would get this kind of really
obvious code right. There is absolutely no question about what the correct
result is, and gcc simply doesn't generate it.

I'm used to gcc sometimes being confused by more complicated things (inline
asms, bitfields etc), but this is really basic code.

The load-store model is fine for a Pentium 4 - those things were not very good
at complex instructions. But it generates horribly big code, and modern x86
chips all want the "operate on memory" version.

> Manually sinking the store to *argv into the if and the else yields

Yeah. And that's pretty horrible. 

> As usual combine doesn't like stores.

Is there some reason this can't just be a peephole pattern?

I really thought that gcc has done this before. 

                       Linus


  parent reply	other threads:[~2011-05-21 18:50 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-21  3:46 [Bug other/49095] New: " 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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-49095-4-SKRClnYklx@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).