This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] Fix PR26898, folding of X +- C1 CMP Y +- C2
- From: "Joseph S. Myers" <joseph at codesourcery dot com>
- To: Richard Guenther <rguenther at suse dot de>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Thu, 30 Mar 2006 13:43:08 +0000 (UTC)
- Subject: Re: [PATCH] Fix PR26898, folding of X +- C1 CMP Y +- C2
- References: <Pine.LNX.4.64.0603301515510.3982@t148.fhfr.qr>
On Thu, 30 Mar 2006, Richard Guenther wrote:
> 2006-03-29 Richard Guenther <rguenther@suse.de>
>
> PR middle-end/26898
> * fold-const.c (fold_comparison): Fold signed comparisons
> of the form X +- C1 CMP Y +- C2.
This miscompiles:
int a = 0, b = __INT_MAX__ - 1;
extern void abort(void);
extern void exit(int);
int
main(void)
{
if (a - 1 > b + 1)
abort();
exit(0);
}
The optimization is only valid if the two constants cancel to one of the
same sign (as in the original "a + 1 < b + 1"; or "a + 1 < b + 2"); not if
you end up adding two constants of the same sign, which may cause overflow
where the original test didn't have any. And for "a + 2 < b + 1" it's OK
to change it to "a + 1 < b", but not to "a < b - 1", since "b - 1" may
overflow.
--
Joseph S. Myers http://www.srcf.ucam.org/~jsm28/gcc/
jsm@polyomino.org.uk (personal mail)
joseph@codesourcery.com (CodeSourcery mail)
jsm28@gcc.gnu.org (Bugzilla assignments and CCs)