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
On Thu, 30 Mar 2006, Joseph S. Myers wrote:
> 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.
Hmm, correct. I'll rework the patch.
Thanks,
Richard.