Bug 14490 - [tree-ssa] Simplify "a - 10 > 150" into "a > 160"
Summary: [tree-ssa] Simplify "a - 10 > 150" into "a > 160"
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: tree-ssa
: P2 enhancement
Target Milestone: 4.1.0
Assignee: Andrew Pinski
URL: http://gcc.gnu.org/ml/gcc-patches/200...
Keywords: missed-optimization, patch
Depends on: 15459 22236
Blocks: 19987
  Show dependency treegraph
 
Reported: 2004-03-08 20:42 UTC by Kazu Hirata
Modified: 2005-07-02 16:25 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-05-08 18:59:53


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kazu Hirata 2004-03-08 20:42:20 UTC
Consider:

void bar (void);

void
foo (int a)
{
  if (a < 100)
    return;
  if (200 < a)
    return;

  if (a - 10 > 150)
    bar ();
}

We know that "a" is between 100 and 200 after the first two ifs.
So we should be able to simplify "a - 10 > 150" into "a > 160".
Here is what I get:

foo (a)
{
  int T.0;

<bb 0>:
  if (a <= 99) goto <L0>; else goto <L1>;

<L0>:;
  return;

<L1>:;
  if (a > 200) goto <L2>; else goto <L3>;

<L2>:;
  return;

<L3>:;
  if (a - 10 > 150) goto <L4>; else goto <L5>;

<L4>:;
  bar () [tail call];

<L5>:;
  return;

}
Comment 1 Andrew Pinski 2004-03-08 20:45:01 UTC
Confirmed.
Comment 2 Andrew Pinski 2004-05-17 01:46:28 UTC
Fold is not doing this optimization.  PR 15459 is not really needed for this but it still will 
be needed for other cases where a variable is used instead of the expression a-10.
Comment 3 Andrew Pinski 2005-06-29 19:37:58 UTC
I think I have a fix.
Comment 4 Andrew Pinski 2005-06-29 20:05:55 UTC
Actually since overflow is undefined we can do this no matter what at least for signed types.
Comment 5 Andrew Pinski 2005-06-29 20:07:44 UTC
(In reply to comment #4)
> Actually since overflow is undefined we can do this no matter what at least for signed types.
And when -fwrapv is not supplied.
Comment 6 Andrew Pinski 2005-06-29 20:57:17 UTC
I found a bug which my patch will make latent (PR 22236).
Comment 7 Andrew Pinski 2005-06-30 04:42:34 UTC
And I found another latent bug which makes it impossible to bootstrap GCC :(
Here is a short testcase for that bug:
_Bool f();
void g(int);
void h (int old_size)
{
  int new_size = old_size, i;
  g(old_size - 1);      
  i = 0;
  while (i < old_size - 1)
  {
    if (f())
    {
      i++;
      continue;
    }
    while (i < old_size - 1)
      i++;
  }
  g(new_size);
}
Comment 8 Andrew Pinski 2005-06-30 06:03:52 UTC
(In reply to comment #7)
> And I found another latent bug which makes it impossible to bootstrap GCC :(
Oh, I found a simple patch which fixes that, bootstrapping the fix with the fix for this bug.
Comment 9 Andrew Pinski 2005-06-30 19:13:48 UTC
Patch posted here: <http://gcc.gnu.org/ml/gcc-patches/2005-06/msg02249.html>.  This might 
improve code gen in SPEC but since I have not tried, I don't know but I know that Richard Earnshaw was 
asking about this.
Comment 11 Andrew Pinski 2005-07-02 16:25:20 UTC
Fixed.