Bug 52254 - VRP does not fold (~a) & N to a ^ N if a has range [0, N]
Summary: VRP does not fold (~a) & N to a ^ N if a has range [0, N]
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.7.0
: P3 enhancement
Target Milestone: 7.0
Assignee: Andrew Pinski
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2012-02-15 00:21 UTC by Andrew Pinski
Modified: 2021-06-03 00:40 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2012-02-15 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Pinski 2012-02-15 00:21:43 UTC
While working on moving fold-const.c bit expression optimizations to a SSA based optimization, I found that if we optimize (a^N)&N to ~a & N, VRP does not change that to just a ^ N.
Simple example:
/* { dg-do compile } */
/* { dg-options "-O2 -fno-tree-ccp -fdump-tree-vrp1" } */

int f(int x)
{
  if (x >= 0 && x <= 3)
    {
      x = (x ^ 3) & 3;
    }
  return x;
}

/* { dg-final { scan-tree-dump-times " & 3;" 0 "vrp1" { xfail *-*-* } } } */
/* { dg-final { cleanup-tree-dump "vrp1" } } */
Comment 1 Andrew Pinski 2012-02-15 00:23:42 UTC
I am going to work on this with my combine patches, I am thinking about allowing VRP to supply a range to the combine stage and we should be able to use that info to do the folding there (in a more common place too).
Comment 2 Richard Biener 2012-02-15 11:21:53 UTC
The combine stage should have access to a valueize() hook as well, to query
SSA propagator/VNs lattice.  That hook can maybe supply this extra info
(I wanted to make VRP info persistent for some time, in SSA_NAME_PTR_INFO
much like we have alignment info there which really is kind of a value-range)
Comment 3 Andrew Pinski 2012-02-16 03:00:22 UTC
(In reply to comment #2)
> The combine stage should have access to a valueize() hook as well, to query
> SSA propagator/VNs lattice.  That hook can maybe supply this extra info
> (I wanted to make VRP info persistent for some time, in SSA_NAME_PTR_INFO
> much like we have alignment info there which really is kind of a value-range)

I was thinking about adding that.  Even using nonzero bits to create a simple range.
Comment 4 Andrew Pinski 2021-06-03 00:40:58 UTC
Was fixed with r7-820 which adds the pattern to match.pd.