public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-2499] expmed: Fix store_integral_bit_field [PR101562]
Date: Fri, 23 Jul 2021 17:56:16 +0000 (GMT)	[thread overview]
Message-ID: <20210723175616.7266D385C41C@sourceware.org> (raw)

https://gcc.gnu.org/g:8408d34570c9fe9f3d22a25a76df2a4c64f08477

commit r12-2499-g8408d34570c9fe9f3d22a25a76df2a4c64f08477
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri Jul 23 19:55:16 2021 +0200

    expmed: Fix store_integral_bit_field [PR101562]
    
    Our documentation says that paradoxical subregs shouldn't appear
    in strict_low_part:
    '(strict_low_part (subreg:M (reg:N R) 0))'
         This expression code is used in only one context: as the
         destination operand of a 'set' expression.  In addition, the
         operand of this expression must be a non-paradoxical 'subreg'
         expression.
    but on the testcase below that triggers UB at runtime
    store_integral_bit_field emits exactly that.
    
    The following patch fixes it by ensuring the requirement is satisfied.
    
    2021-07-23  Jakub Jelinek  <jakub@redhat.com>
    
            PR rtl-optimization/101562
            * expmed.c (store_integral_bit_field): Only use movstrict_optab
            if the operand isn't paradoxical.
    
            * gcc.c-torture/compile/pr101562.c: New test.

Diff:
---
 gcc/expmed.c                                   |  5 ++++-
 gcc/testsuite/gcc.c-torture/compile/pr101562.c | 21 +++++++++++++++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/gcc/expmed.c b/gcc/expmed.c
index 1fb63170be9..3143f38e057 100644
--- a/gcc/expmed.c
+++ b/gcc/expmed.c
@@ -921,7 +921,10 @@ store_integral_bit_field (rtx op0, opt_scalar_int_mode op0_mode,
 	}
 
       subreg_off = bitnum / BITS_PER_UNIT;
-      if (validate_subreg (fieldmode, GET_MODE (arg0), arg0, subreg_off))
+      if (validate_subreg (fieldmode, GET_MODE (arg0), arg0, subreg_off)
+	  /* STRICT_LOW_PART must have a non-paradoxical subreg as
+	     operand.  */
+	  && !paradoxical_subreg_p (fieldmode, GET_MODE (arg0)))
 	{
 	  arg0 = gen_rtx_SUBREG (fieldmode, arg0, subreg_off);
 
diff --git a/gcc/testsuite/gcc.c-torture/compile/pr101562.c b/gcc/testsuite/gcc.c-torture/compile/pr101562.c
new file mode 100644
index 00000000000..ea4a5f7103a
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/compile/pr101562.c
@@ -0,0 +1,21 @@
+/* PR rtl-optimization/101562 */
+
+struct S { char c; };
+void baz (struct S a, struct S b);
+
+void
+foo (void)
+{
+  struct S x[1];
+  *(short *)&x[0] = 256;
+  baz (x[0], x[1]);
+}
+
+void
+bar (void)
+{
+  struct S x[1];
+  x[0].c = 0;
+  x[1].c = 1;
+  baz (x[0], x[1]);
+}


                 reply	other threads:[~2021-07-23 17:56 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20210723175616.7266D385C41C@sourceware.org \
    --to=jakub@gcc.gnu.org \
    --cc=gcc-cvs@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).