public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* RFA: Fix uint128_t range checking in VRP
@ 2012-12-23 16:49 Richard Sandiford
  2012-12-30 19:24 ` Richard Biener
  0 siblings, 1 reply; 2+ messages in thread
From: Richard Sandiford @ 2012-12-23 16:49 UTC (permalink / raw)
  To: gcc-patches

The minimum uint128_t value is an all-zeros double_int and the maximum
value is an all-ones double_int.  Truncating these values to int and
then sign-extending them gives the same all-zeros and all-ones values,
so tree-vrp.c:range_fits_type_p concludes that uint128_t fits in an int.

This showed up as a miscompilation of __fractutasf on mips64-linux-gnu,
which in turn showed up in convert-float-4.c.

There was already code to handle this problem when changing the sign
and keeping the precision, but I think the rule applies regardless
of precision.

Tested on x86_64-linux-gnu and mips64-linux-gnu.  OK to install?

Richard


gcc/
	* tree-vrp.c (range_fits_type_p): Require the MSB of the double_int
	to be clear for sign changes.

gcc/testsuite/
	* gcc.dg/torture/fp-int-convert-2.c: New test.

Index: gcc/tree-vrp.c
===================================================================
--- gcc/tree-vrp.c	2012-12-23 11:50:39.046419271 +0000
+++ gcc/tree-vrp.c	2012-12-23 11:51:17.814677008 +0000
@@ -8766,9 +8766,11 @@ range_fits_type_p (value_range_t *vr, un
       || TREE_CODE (vr->max) != INTEGER_CST)
     return false;
 
-  /* For precision-preserving sign-changes the MSB of the double-int
-     has to be clear.  */
-  if (src_precision == precision
+  /* For sign changes, the MSB of the double_int has to be clear.
+     An unsigned value with its MSB set cannot be represented by
+     a signed double_int, while a negative value cannot be represented
+     by an unsigned double_int.  */
+  if (TYPE_UNSIGNED (src_type) != unsigned_p
       && (TREE_INT_CST_HIGH (vr->min) | TREE_INT_CST_HIGH (vr->max)) < 0)
     return false;
 
Index: gcc/testsuite/gcc.dg/torture/fp-int-convert-2.c
===================================================================
--- /dev/null	2012-12-03 19:06:31.446621561 +0000
+++ gcc/testsuite/gcc.dg/torture/fp-int-convert-2.c	2012-12-23 13:46:35.449077882 +0000
@@ -0,0 +1,18 @@
+/* { dg-do run } */
+/* { dg-require-effective-target int128 } */
+
+extern void abort (void);
+
+float __attribute__((noinline))
+f (__uint128_t x)
+{
+  return x + 1;
+}
+
+int
+main (void)
+{
+  if (f (0xffffffffu) == 0)
+    abort ();
+  return 0;
+}

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: RFA: Fix uint128_t range checking in VRP
  2012-12-23 16:49 RFA: Fix uint128_t range checking in VRP Richard Sandiford
@ 2012-12-30 19:24 ` Richard Biener
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2012-12-30 19:24 UTC (permalink / raw)
  To: gcc-patches, rdsandiford

On Sun, Dec 23, 2012 at 5:48 PM, Richard Sandiford
<rdsandiford@googlemail.com> wrote:
> The minimum uint128_t value is an all-zeros double_int and the maximum
> value is an all-ones double_int.  Truncating these values to int and
> then sign-extending them gives the same all-zeros and all-ones values,
> so tree-vrp.c:range_fits_type_p concludes that uint128_t fits in an int.
>
> This showed up as a miscompilation of __fractutasf on mips64-linux-gnu,
> which in turn showed up in convert-float-4.c.
>
> There was already code to handle this problem when changing the sign
> and keeping the precision, but I think the rule applies regardless
> of precision.
>
> Tested on x86_64-linux-gnu and mips64-linux-gnu.  OK to install?

Ok.

Thanks,
Richard.

> Richard
>
>
> gcc/
>         * tree-vrp.c (range_fits_type_p): Require the MSB of the double_int
>         to be clear for sign changes.
>
> gcc/testsuite/
>         * gcc.dg/torture/fp-int-convert-2.c: New test.
>
> Index: gcc/tree-vrp.c
> ===================================================================
> --- gcc/tree-vrp.c      2012-12-23 11:50:39.046419271 +0000
> +++ gcc/tree-vrp.c      2012-12-23 11:51:17.814677008 +0000
> @@ -8766,9 +8766,11 @@ range_fits_type_p (value_range_t *vr, un
>        || TREE_CODE (vr->max) != INTEGER_CST)
>      return false;
>
> -  /* For precision-preserving sign-changes the MSB of the double-int
> -     has to be clear.  */
> -  if (src_precision == precision
> +  /* For sign changes, the MSB of the double_int has to be clear.
> +     An unsigned value with its MSB set cannot be represented by
> +     a signed double_int, while a negative value cannot be represented
> +     by an unsigned double_int.  */
> +  if (TYPE_UNSIGNED (src_type) != unsigned_p
>        && (TREE_INT_CST_HIGH (vr->min) | TREE_INT_CST_HIGH (vr->max)) < 0)
>      return false;
>
> Index: gcc/testsuite/gcc.dg/torture/fp-int-convert-2.c
> ===================================================================
> --- /dev/null   2012-12-03 19:06:31.446621561 +0000
> +++ gcc/testsuite/gcc.dg/torture/fp-int-convert-2.c     2012-12-23 13:46:35.449077882 +0000
> @@ -0,0 +1,18 @@
> +/* { dg-do run } */
> +/* { dg-require-effective-target int128 } */
> +
> +extern void abort (void);
> +
> +float __attribute__((noinline))
> +f (__uint128_t x)
> +{
> +  return x + 1;
> +}
> +
> +int
> +main (void)
> +{
> +  if (f (0xffffffffu) == 0)
> +    abort ();
> +  return 0;
> +}

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-12-30 19:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-12-23 16:49 RFA: Fix uint128_t range checking in VRP Richard Sandiford
2012-12-30 19:24 ` Richard Biener

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).