Bug 23603 - VRP does not say range for a in a = b == c; is [0,1]
Summary: VRP does not say range for a in a = b == c; is [0,1]
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.1.0
: P2 enhancement
Target Milestone: 4.3.0
Assignee: Andrew Pinski
URL:
Keywords: missed-optimization
Depends on: 23604
Blocks:
  Show dependency treegraph
 
Reported: 2005-08-28 17:20 UTC by Andrew Pinski
Modified: 2007-01-08 11:21 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-12-27 04:33:11


Attachments
patch which I need to test (518 bytes, patch)
2005-08-29 00:42 UTC, Andrew Pinski
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Pinski 2005-08-28 17:20:16 UTC
Example:
int f(int i)
{
  int t = i == 1;
  int g = t == 2;
  int h = g == 3;
  return h;
}
We should convert this to return 0 on the tree level but currently we get:
return i == 1 == 2 == 3;
in final_cleanup
Comment 1 Andrew Pinski 2005-08-28 17:22:34 UTC
The fix would be instead of setting the range to varrying in extract_range_from_comparison, set it to 
[0,1].
Comment 2 Andrew Pinski 2005-08-28 17:25:25 UTC
I have a fix.
Comment 3 Andrew Pinski 2005-08-29 00:42:24 UTC
Created attachment 9610 [details]
patch which I need to test

This patch fixes the problem but exposes PR 23604 so I cannot bootstrap this
test until PR 23604 is fixed as PR 23604 is exposed in compiling GCC.
Comment 4 Andrew Pinski 2005-08-29 00:49:34 UTC
This really does depend on PR 23604 as it exposes that latent bug while bootstrapping.
Comment 5 Andrew Pinski 2005-10-03 00:43:09 UTC
I just tried to bootstrap the patch on x86_64-linux-gnu and I think I ran into another VRP bug.
Comment 6 Andrew Pinski 2005-10-03 02:39:34 UTC
(In reply to comment #5)
> I just tried to bootstrap the patch on x86_64-linux-gnu and I think I ran into
> another VRP bug.
I could not find it in cc1files and there was only one extra "Folding predicate" in the whole cc1files :(.
I know it is in the gnat1 front-end but I really don't want to do the dumps for that :(.
Comment 7 Andrew Pinski 2005-10-04 19:44:37 UTC
(In reply to comment #6)
> I know it is in the gnat1 front-end but I really don't want to do the dumps for
> that :(.

I figured out how to fix the problem but to me it seems like a front-end bug in that it does not set TREE_PRECISSION on its boolean type correctly.
Comment 8 Andrew Pinski 2005-10-23 00:06:39 UTC
The patch above is almost correct and I just lost the correct one as my laptop is dead so I am no longer working on this.
Comment 9 Andrew Pinski 2006-08-21 06:11:53 UTC
I am going to try to get this done for 4.3.0.
Comment 10 Richard Biener 2007-01-07 17:44:26 UTC
In extract_range_from_binary we have another case where we drop to varying:

  /* For integer ranges, apply the operation to each end of the
     range and see what we end up with.  */
  if (code == TRUTH_ANDIF_EXPR
      || code == TRUTH_ORIF_EXPR
      || code == TRUTH_AND_EXPR
      || code == TRUTH_OR_EXPR)
    {
...
      else
        {
          set_value_range_to_varying (vr);
          return;
        }
    }

But the latent problems seem to be fixed, the following (simple) patch bootstrapped and regtested ok for me:

*************** extract_range_from_comparison (value_ran
*** 1920,1926 ****
        set_value_range (vr, VR_RANGE, val, val, vr->equiv);
      }
    else
!     set_value_range_to_varying (vr);
  }


--- 1925,1933 ----
        set_value_range (vr, VR_RANGE, val, val, vr->equiv);
      }
    else
!     /* The result of a comparison is always true or false.  */
!     set_value_range (vr, VR_RANGE, build_int_cst (TREE_TYPE (expr), 0),
!                    build_int_cst (TREE_TYPE (expr), 1), vr->equiv);
  }

Comment 11 Richard Biener 2007-01-08 11:20:17 UTC
Subject: Bug 23603

Author: rguenth
Date: Mon Jan  8 11:20:00 2007
New Revision: 120578

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=120578
Log:
2007-01-08  Richard Guenther  <rguenther@suse.de>

        PR tree-optimization/23603
        * tree-vrp.c (set_value_range_to_truthvalue): New function.
        (extract_range_from_binary): Fall back to truthvalue instead of
        varying for TRUTH_*_EXPR.
        (extract_range_from_comparison): Fall back to truthvalue instead of
        varying.
        (vrp_visit_phi_node): Don't adjust new range bounds to +INF/-INF
        if all visited PHI values were constant.

        * gcc.dg/tree-ssa/vrp31.c: New testcase.
        * gcc.dg/tree-ssa/vrp32.c: Likewise.

Added:
    trunk/gcc/testsuite/gcc.dg/tree-ssa/vrp31.c
    trunk/gcc/testsuite/gcc.dg/tree-ssa/vrp32.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-vrp.c

Comment 12 Richard Biener 2007-01-08 11:21:36 UTC
Fixed.