public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Mike Stump <mikestump@comcast.net>
To: GCC Patches <gcc-patches@gcc.gnu.org>
Cc: Richard Sandiford <rdsandiford@googlemail.com>,
	Eric Botcazou <ebotcazou@adacore.com>
Subject: shift/extract SHIFT_COUNT_TRUNCATED  combine bug
Date: Tue, 08 Apr 2014 20:09:00 -0000	[thread overview]
Message-ID: <8F47DDC3-F9FE-4E94-90F7-3A16A3FD47CE@comcast.net> (raw)

Something broke in the compiler to cause combine to incorrectly optimize:

(insn 12 11 13 3 (set (reg:SI 604 [ D.6102 ])
        (lshiftrt:SI (subreg/s/u:SI (reg/v:DI 601 [ x ]) 0)
            (reg:SI 602 [ D.6103 ]))) t.c:47 4436 {lshrsi3}
     (expr_list:REG_DEAD (reg:SI 602 [ D.6103 ])
        (nil)))
(insn 13 12 14 3 (set (reg:SI 605)
        (and:SI (reg:SI 604 [ D.6102 ])
            (const_int 1 [0x1]))) t.c:47 3658 {andsi3}
     (expr_list:REG_DEAD (reg:SI 604 [ D.6102 ])
        (nil)))
(insn 14 13 15 3 (set (reg:DI 599 [ D.6102 ])
        (zero_extend:DI (reg:SI 605))) t.c:47 4616 {zero_extendsidi2}
     (expr_list:REG_DEAD (reg:SI 605)
        (nil)))

into:

(insn 11 10 12 3 (set (reg:SI 602 [ D.6103 ])
        (not:SI (subreg:SI (reg:DI 595 [ D.6102 ]) 0))) t.c:47 3732 {one_cmplsi2}
     (expr_list:REG_DEAD (reg:DI 595 [ D.6102 ])
        (nil)))
(note 12 11 13 3 NOTE_INSN_DELETED)
(note 13 12 14 3 NOTE_INSN_DELETED)
(insn 14 13 15 3 (set (reg:DI 599 [ D.6102 ])
        (zero_extract:DI (reg/v:DI 601 [ x ])
            (const_int 1 [0x1])
            (reg:SI 602 [ D.6103 ]))) t.c:47 4668 {c2_extzvdi}
     (expr_list:REG_DEAD (reg:SI 602 [ D.6103 ])
        (nil)))

This shows up in:

  FAIL: gcc.c-torture/execute/builtin-bitops-1.c execution,  -Og -g

for me.

diff --git a/gcc/combine.c b/gcc/combine.c
index 708691f..c1f50ff 100644
--- a/gcc/combine.c
+++ b/gcc/combine.c
@@ -7245,6 +7245,18 @@ make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
       extraction_mode = insn.field_mode;
     }
 
+  /* On a SHIFT_COUNT_TRUNCATED machine, we can't promote the mode of
+     the extract to a larger size on a variable extract, as previously
+     the position might have been optimized to change a bit of the
+     index of the starting bit that would have been ignored before,
+     but, with a larger mode, will then not be.  If we wanted to do
+     this, we'd have to mask out those bits or prove that those bits
+     are 0.  */
+  if (SHIFT_COUNT_TRUNCATED
+      && pos_rtx
+      && GET_MODE_BITSIZE (extraction_mode) > GET_MODE_BITSIZE (mode))
+    extraction_mode = mode;
+
   /* Never narrow an object, since that might not be safe.  */
 
   if (mode != VOIDmode

is sufficient to never widen variable extracts on SHIFT_COUNT_TRUNCATED machines.  So, the question is, how did people expect this to work?  I didn’t spot what changed recently to cause the bad code-gen.  The optimization of sub into not is ok, despite how funny it looks, because is feeds into extract which we know by SHIFT_COUNT_TRUNCATED is safe.

Is the patch a reasonable way to fix this?

             reply	other threads:[~2014-04-08 20:09 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-08 20:09 Mike Stump [this message]
2015-01-12 22:25 ` Jeff Law
2015-01-13 10:11   ` Richard Biener
2015-01-13 17:00     ` Jeff Law
2015-01-13 18:25     ` Segher Boessenkool
2015-01-14  9:24       ` Richard Biener
2015-01-14 14:35         ` Segher Boessenkool

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=8F47DDC3-F9FE-4E94-90F7-3A16A3FD47CE@comcast.net \
    --to=mikestump@comcast.net \
    --cc=ebotcazou@adacore.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=rdsandiford@googlemail.com \
    /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).