Bug 26898 - Fold does not fold signed + CST1 CMP signed + CST2
Summary: Fold does not fold signed + CST1 CMP signed + CST2
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.2.0
: P3 enhancement
Target Milestone: 4.3.0
Assignee: Richard Biener
URL:
Keywords: missed-optimization
Depends on:
Blocks: 26900
  Show dependency treegraph
 
Reported: 2006-03-28 13:49 UTC by Richard Biener
Modified: 2006-10-21 13:21 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-04-05 13:47:01


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Richard Biener 2006-03-28 13:49:12 UTC
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.
Comment 1 Richard Biener 2006-03-28 13:59:43 UTC
Testcase:

void foo0(int i0, int i1)
{
  if (!(i0 + 1 < i1 + 1 == i0 < i1))
    link_error ();
}

Comment 2 Andrew Pinski 2006-03-30 21:49:44 UTC
Confirmed, only when CST1 == CST2 does this work based on:
http://gcc.gnu.org/ml/gcc-patches/2006-03/msg01746.html
Comment 3 Richard Biener 2006-03-30 22:12:46 UTC
It also works for combined CST < CST1 or CST2, i.e.

  a +- c1 CMP b +- c2 to a CMP b +- c3 where c3 < c2
Comment 4 Richard Biener 2006-04-05 13:47:01 UTC
This is mine.  And I have a patch posted.
Comment 5 patchapp@dberlin.org 2006-04-05 13:50:13 UTC
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
Comment 6 Richard Biener 2006-10-21 13:21:18 UTC
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

Comment 7 Richard Biener 2006-10-21 13:21:22 UTC
Fixed.