]> gcc.gnu.org Git - gcc.git/commitdiff
expmed: Fix store_integral_bit_field [PR101562]
authorJakub Jelinek <jakub@redhat.com>
Fri, 23 Jul 2021 17:55:16 +0000 (19:55 +0200)
committerJakub Jelinek <jakub@redhat.com>
Tue, 10 May 2022 08:14:26 +0000 (10:14 +0200)
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.

(cherry picked from commit 8408d34570c9fe9f3d22a25a76df2a4c64f08477)

gcc/expmed.c
gcc/testsuite/gcc.c-torture/compile/pr101562.c [new file with mode: 0644]

index 96ced5c8c8b61fa101fd547d3bb682464aa35e78..65d519fe5987ca250d1a084271765dca8ba73072 100644 (file)
@@ -917,7 +917,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 (file)
index 0000000..ea4a5f7
--- /dev/null
@@ -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]);
+}
This page took 0.066625 seconds and 5 git commands to generate.