From c078a43735c62e3f90ac80ba1ae01e6d0b83baba Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Thu, 22 Dec 2005 04:03:32 +0000 Subject: [PATCH] re PR middle-end/23518 (some gcc optimizations do not take overflow into account with -fwrapv) gcc/ PR tree-optimization/23518 * fold-const.c (make_range): Don't move a constant to the other side of the comparison if the type is signed and -fwrapv is given. gcc/testsuite/ PR tree-optimization/23518 * testsuite/gcc.dg/pr23518.c: New. From-SVN: r108940 --- gcc/ChangeLog | 7 +++++++ gcc/fold-const.c | 5 +++++ gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gcc.dg/pr23518.c | 21 +++++++++++++++++++++ 4 files changed, 38 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/pr23518.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index cdc10e038b6..b1b50aa7398 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2005-12-22 Kazu Hirata + + PR tree-optimization/23518 + * fold-const.c (make_range): Don't move a constant to the + other side of the comparison if the type is signed and -fwrapv + is given. + 2005-12-22 Kazu Hirata * tree-vrp.c (extract_range_from_binary_expr): Clean up uses diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 33e27af59f0..b801f2ac9e9 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -3836,6 +3836,11 @@ make_range (tree exp, int *pin_p, tree *plow, tree *phigh) if (TREE_CODE (arg1) != INTEGER_CST) break; + /* If flag_wrapv and ARG0_TYPE is signed, then we cannot + move a constant to the other side. */ + if (flag_wrapv && !TYPE_UNSIGNED (arg0_type)) + break; + /* If EXP is signed, any overflow in the computation is undefined, so we don't worry about it so long as our computations on the bounds don't overflow. For unsigned, overflow is defined diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index b788e59315f..acfd2c89d52 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2005-12-22 Kazu Hirata + + PR tree-optimization/23518 + * testsuite/gcc.dg/pr23518.c: New. + 2005-12-21 Mike Stump * gcc.dg/attr-weakref-1.c: Really skip on darwin. diff --git a/gcc/testsuite/gcc.dg/pr23518.c b/gcc/testsuite/gcc.dg/pr23518.c new file mode 100644 index 00000000000..3c6bd2754e6 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr23518.c @@ -0,0 +1,21 @@ +/* PR tree-optimization/23518. + make_range used to transform a + 1 < 0 into a < -1 even when a is + signed and -fwrapv is given. Make sure that no longer happens. */ + +/* { dg-do run } */ +/* { dg-options "-O2 -fwrapv" } */ + +#include + +extern void abort (void); +extern void exit (int); + +int +main (void) +{ + int a = INT_MAX; + if ((a < 0) || (a + 1 < 0)) + exit (0); + + abort (); +} -- 2.30.2