fold x ^ y to 0 if x == y

Jeff Law law@redhat.com
Wed Oct 5 19:39:00 GMT 2016


On 07/20/2016 04:07 PM, Prathamesh Kulkarni wrote:
>>
>> Not record both equivalences.  This might break the testcase it was
>> introduced for (obviously).  Which is why I CCed Jeff for his opinion.
> Well, folding happens for x - y, if x == y.
>
> int f(int x, int y)
> {
>   int ret;
>   if (x == y)
>     ret = x - y;
>   else
>     ret = 1;
>
>   return ret;
> }
This should be caught by DOM as well.

One approach to this (and the version with XOR) in DOM is to fold the 
statement at each propagation step if something changed.  It's going to 
be very rare that we propagate a value into > 1 place in a statement, so 
that shouldn't be expensive.

Some very quick and dirty tests show that can work.



> However it appears vrp fails to notice the equality for the following test-case,
> and sets range for ret to VARYING.
>
> int f(int x, int y, int a, int b)
> {
>   int ret = 10;
>   if (a == x
>       && b == y
>       && a == b)
>     ret = x - y;
>
>   return ret;
> }
As you see in the dumps, this turns into a series of comparisons feeding 
a logical which then feeds the conditional.  Untangling that is more 
difficult.  DOM has some support for that, but it's mostly in the threader.


Jeff



More information about the Gcc-patches mailing list