Fold at the moment does not fold comparisons involving signed variables with a constant offset. Like we get from number-of-iterations analysis for int foo0(int i0, int i1) { int i, j = 0; for (i=i0; i<=i1+1; ++i) ++j; return j; } where niter->may_be_zero ends up as i0 + 1 < i1 + 2 which can be folded to i0 < i1 + 1 because signed overflow is undefined. And I have a patch for this.
Testcase: void foo0(int i0, int i1) { if (!(i0 + 1 < i1 + 1 == i0 < i1)) link_error (); }
Confirmed, only when CST1 == CST2 does this work based on: http://gcc.gnu.org/ml/gcc-patches/2006-03/msg01746.html
It also works for combined CST < CST1 or CST2, i.e. a +- c1 CMP b +- c2 to a CMP b +- c3 where c3 < c2
This is mine. And I have a patch posted.
Subject: Bug number PR26898 A patch for this bug has been added to the patch tracker. The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2006-04/msg00190.html
Subject: Bug 26898 Author: rguenth Date: Sat Oct 21 13:21:06 2006 New Revision: 117931 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=117931 Log: 2006-10-21 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. * gcc.dg/torture/pr26898-1.c: New testcase. * gcc.dg/torture/pr26898-2.c: Likewise. Added: trunk/gcc/testsuite/gcc.dg/torture/pr26898-1.c trunk/gcc/testsuite/gcc.dg/torture/pr26898-2.c Modified: trunk/gcc/ChangeLog trunk/gcc/fold-const.c trunk/gcc/testsuite/ChangeLog
Fixed.