]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
combine: Punt on out of range rotate counts [PR93505]
authorJakub Jelinek <jakub@redhat.com>
Thu, 30 Jan 2020 20:28:17 +0000 (21:28 +0100)
committerJakub Jelinek <jakub@redhat.com>
Thu, 13 Feb 2020 20:32:23 +0000 (21:32 +0100)
What happens on this testcase is with the out of bounds rotate we get:
Trying 13 -> 16:
   13: r129:SI=r132:DI#0<-<0x20
      REG_DEAD r132:DI
   16: r123:DI=r129:SI<0
      REG_DEAD r129:SI
Successfully matched this instruction:
(set (reg/v:DI 123 [ <retval> ])
    (const_int 0 [0]))
during combine.  So, perhaps we could also change simplify-rtx.c to punt
if it is out of bounds rather than trying to optimize anything.
Or, but probably GCC11 material, if we decide that ROTATE/ROTATERT doesn't
have out of bounds counts or introduce targetm.rotate_truncation_mask,
we should truncate the argument instead of punting.
Punting is better for backports though.

2020-01-30  Jakub Jelinek  <jakub@redhat.com>

PR middle-end/93505
* combine.c (simplify_comparison) <case ROTATE>: Punt on out of range
rotate counts.

* gcc.c-torture/compile/pr93505.c: New test.

gcc/ChangeLog
gcc/combine.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.c-torture/compile/pr93505.c [new file with mode: 0644]

index ca09488b59d17ce086cfc05bf39d44200db96f6c..0d8e888ec3ac164d78fa7e9d80a8116befd250b7 100644 (file)
@@ -1,6 +1,12 @@
 2020-02-13  Jakub Jelinek  <jakub@redhat.com>
 
        Backported from mainline
+       2020-01-30  Jakub Jelinek  <jakub@redhat.com>
+
+       PR middle-end/93505
+       * combine.c (simplify_comparison) <case ROTATE>: Punt on out of range
+       rotate counts.
+
        2020-01-28  Jakub Jelinek  <jakub@redhat.com>
 
        PR target/93418
index 4de759a8e6bf7cb5b13af592fbb3fcd1b9644929..601943d6bb0e3b25e2619ba086acee590e935b9a 100644 (file)
@@ -12424,7 +12424,8 @@ simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
             bit.  This will be converted into a ZERO_EXTRACT.  */
          if (const_op == 0 && sign_bit_comparison_p
              && CONST_INT_P (XEXP (op0, 1))
-             && mode_width <= HOST_BITS_PER_WIDE_INT)
+             && mode_width <= HOST_BITS_PER_WIDE_INT
+             && UINTVAL (XEXP (op0, 1)) < mode_width)
            {
              op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
                                            (HOST_WIDE_INT_1U
index 62df3f97f49e1dfa122a6b1ea1d7ea9a4bdd8b5c..384f34a41ca7f1ff2d4bfad571efbac58538ac08 100644 (file)
@@ -1,6 +1,11 @@
 2020-02-13  Jakub Jelinek  <jakub@redhat.com>
 
        Backported from mainline
+       2020-01-30  Jakub Jelinek  <jakub@redhat.com>
+
+       PR middle-end/93505
+       * gcc.c-torture/compile/pr93505.c: New test.
+
        2020-01-29  Jakub Jelinek  <jakub@redhat.com>
 
        PR c++/91118
diff --git a/gcc/testsuite/gcc.c-torture/compile/pr93505.c b/gcc/testsuite/gcc.c-torture/compile/pr93505.c
new file mode 100644 (file)
index 0000000..0627962
--- /dev/null
@@ -0,0 +1,15 @@
+/* PR middle-end/93505 */
+
+unsigned a;
+
+unsigned
+foo (unsigned x)
+{
+  unsigned int y = 32 - __builtin_bswap64 (-a);
+  /* This would be UB (x << 32) at runtime.  Ensure we don't
+     invoke UB in the compiler because of that (visible with
+     bootstrap-ubsan).  */
+  x = x << y | x >> (-y & 31);
+  x >>= 31;
+  return x;
+}