[backport gcc-4.8/trunk r194800, also fixes parts of PR54120 ] From: Richard Sandiford Subject: RFA: Fix uint128_t range checking in VRP Date: Sun, 23 Dec 2012 16:48:59 +0000 List-Archive: 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/ 2013-01-02 Richard Sandiford * tree-vrp.c (range_fits_type_p): Require the MSB of the double_int to be clear for sign changes. gcc/testsuite/ 2013-01-02 Richard Sandiford * gcc.dg/torture/fp-int-convert-2.c: New test. --- gcc-4.7.2/gcc/testsuite/gcc.dg/torture/fp-int-convert-2.c.~1~ 1970-01-01 01:00:00.000000000 +0100 +++ gcc-4.7.2/gcc/testsuite/gcc.dg/torture/fp-int-convert-2.c 2013-02-02 15:03:54.079100877 +0100 @@ -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; +} --- gcc-4.7.2/gcc/tree-vrp.c.~1~ 2012-09-07 15:04:38.000000000 +0200 +++ gcc-4.7.2/gcc/tree-vrp.c 2013-02-02 15:03:54.079100877 +0100 @@ -7378,9 +7378,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;