range-op: Fix up ABSU_EXPR handling [PR113756]

Message ID ZcKZz7PBdlnJxzOM@tucnak
State Unresolved
Headers
Series range-op: Fix up ABSU_EXPR handling [PR113756] |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Jakub Jelinek Feb. 6, 2024, 8:42 p.m. UTC
  Hi!

ABSU_EXPR unary expr is special because it has a signed integer
argument and unsigned integer result (of the same precision).

The following testcase is miscompiled since ABSU_EXPR handling has
been added to range-op because it uses widest_int::from with the
result sign (i.e. UNSIGNED) rather than the operand sign (i.e. SIGNED),
so e.g. for the 32-bit int argument mask ends up 0xffffffc1 or something
similar and even when it has most significant bit in the precision set,
in widest_int (tree-ssa-ccp.cc really should stop using widest_int, but
that is I think stage1 task) it doesn't appear to be negative and so
bit_value_unop ABSU_EXPR doesn't set the resulting mask/value from
oring of the argument and its negation.

Fixed thusly, not doing that for GIMPLE_BINARY_RHS because I don't know
about a binary op that would need something similar.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2024-02-06  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/113756
	* range-op.cc (update_known_bitmask): For GIMPLE_UNARY_RHS,
	use TYPE_SIGN (lh.type ()) instead of sign for widest_int::from
	of lh_bits value and mask.

	* gcc.dg/pr113756.c: New test.


	Jakub
  

Comments

Aldy Hernandez Feb. 7, 2024, 9:40 a.m. UTC | #1
LGTM.  Up to the release managers.

Thanks for tracking this down.
Aldy

On Tue, Feb 6, 2024 at 9:43 PM Jakub Jelinek <jakub@redhat.com> wrote:
>
> Hi!
>
> ABSU_EXPR unary expr is special because it has a signed integer
> argument and unsigned integer result (of the same precision).
>
> The following testcase is miscompiled since ABSU_EXPR handling has
> been added to range-op because it uses widest_int::from with the
> result sign (i.e. UNSIGNED) rather than the operand sign (i.e. SIGNED),
> so e.g. for the 32-bit int argument mask ends up 0xffffffc1 or something
> similar and even when it has most significant bit in the precision set,
> in widest_int (tree-ssa-ccp.cc really should stop using widest_int, but
> that is I think stage1 task) it doesn't appear to be negative and so
> bit_value_unop ABSU_EXPR doesn't set the resulting mask/value from
> oring of the argument and its negation.
>
> Fixed thusly, not doing that for GIMPLE_BINARY_RHS because I don't know
> about a binary op that would need something similar.
>
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
>
> 2024-02-06  Jakub Jelinek  <jakub@redhat.com>
>
>         PR tree-optimization/113756
>         * range-op.cc (update_known_bitmask): For GIMPLE_UNARY_RHS,
>         use TYPE_SIGN (lh.type ()) instead of sign for widest_int::from
>         of lh_bits value and mask.
>
>         * gcc.dg/pr113756.c: New test.
>
> --- gcc/range-op.cc.jj  2024-01-03 11:51:28.199777434 +0100
> +++ gcc/range-op.cc     2024-02-06 16:51:55.549127825 +0100
> @@ -435,8 +435,10 @@ update_known_bitmask (irange &r, tree_co
>        bit_value_unop (code, sign, prec, &widest_value, &widest_mask,
>                       TYPE_SIGN (lh.type ()),
>                       TYPE_PRECISION (lh.type ()),
> -                     widest_int::from (lh_bits.value (), sign),
> -                     widest_int::from (lh_bits.mask (), sign));
> +                     widest_int::from (lh_bits.value (),
> +                                       TYPE_SIGN (lh.type ())),
> +                     widest_int::from (lh_bits.mask (),
> +                                       TYPE_SIGN (lh.type ())));
>        break;
>      case GIMPLE_BINARY_RHS:
>        bit_value_binop (code, sign, prec, &widest_value, &widest_mask,
> --- gcc/testsuite/gcc.dg/pr113756.c.jj  2024-02-06 17:00:52.835679796 +0100
> +++ gcc/testsuite/gcc.dg/pr113756.c     2024-02-06 17:00:31.159980326 +0100
> @@ -0,0 +1,36 @@
> +/* PR tree-optimization/113756 */
> +/* { dg-do run { target int32plus } } */
> +/* { dg-options "-O2" } */
> +
> +int d, e, i, k, l = -8;
> +signed char h, j;
> +
> +int
> +bar (int n, int o, int p3)
> +{
> +  int a = o - p3, b = n - p3, c = a + b, f = -b, g = c < 0 ? -c : c;
> +  return a <= f && a <= g ? o : p3;
> +}
> +
> +void
> +foo (int *n, unsigned short o)
> +{
> +  unsigned p = 8896;
> +  for (; e >= 0; e--)
> +    p = 5377;
> +  for (; h <= 0; h++)
> +    for (; j <= 0; j++)
> +      {
> +       *n = 1611581749;
> +       i = bar (34, p - 5294, *n - 1611581687);
> +       k = i + p + 65535 + o + *n - 1611718251;
> +       if (k != 0)
> +         __builtin_abort ();
> +      }
> +}
> +
> +int
> +main ()
> +{
> +  foo (&l, l);
> +}
>
>         Jakub
>
  

Patch

--- gcc/range-op.cc.jj	2024-01-03 11:51:28.199777434 +0100
+++ gcc/range-op.cc	2024-02-06 16:51:55.549127825 +0100
@@ -435,8 +435,10 @@  update_known_bitmask (irange &r, tree_co
       bit_value_unop (code, sign, prec, &widest_value, &widest_mask,
 		      TYPE_SIGN (lh.type ()),
 		      TYPE_PRECISION (lh.type ()),
-		      widest_int::from (lh_bits.value (), sign),
-		      widest_int::from (lh_bits.mask (), sign));
+		      widest_int::from (lh_bits.value (),
+					TYPE_SIGN (lh.type ())),
+		      widest_int::from (lh_bits.mask (),
+					TYPE_SIGN (lh.type ())));
       break;
     case GIMPLE_BINARY_RHS:
       bit_value_binop (code, sign, prec, &widest_value, &widest_mask,
--- gcc/testsuite/gcc.dg/pr113756.c.jj	2024-02-06 17:00:52.835679796 +0100
+++ gcc/testsuite/gcc.dg/pr113756.c	2024-02-06 17:00:31.159980326 +0100
@@ -0,0 +1,36 @@ 
+/* PR tree-optimization/113756 */
+/* { dg-do run { target int32plus } } */
+/* { dg-options "-O2" } */
+
+int d, e, i, k, l = -8;
+signed char h, j;
+
+int
+bar (int n, int o, int p3)
+{
+  int a = o - p3, b = n - p3, c = a + b, f = -b, g = c < 0 ? -c : c;
+  return a <= f && a <= g ? o : p3;
+}
+
+void
+foo (int *n, unsigned short o)
+{
+  unsigned p = 8896;
+  for (; e >= 0; e--)
+    p = 5377;
+  for (; h <= 0; h++)
+    for (; j <= 0; j++)
+      {
+	*n = 1611581749;
+	i = bar (34, p - 5294, *n - 1611581687);
+	k = i + p + 65535 + o + *n - 1611718251;
+	if (k != 0)
+	  __builtin_abort ();
+      }
+}
+
+int
+main ()
+{
+  foo (&l, l);
+}