On 7/26/2021 6:45 PM, Victor Tong via Gcc-patches wrote:
This change enables the "t1 != 0" check to be optimized away in this code:

int x1 = 0;
unsigned int x2 = 1;

int main ()
{
     int t1 = x1*(1/(x2+x2));
     if (t1 != 0) __builtin_abort();
     return 0;
}

The change utilizes the VRP framework to propagate the get_nonzero_bits information from the 
"x2+x2" expression to the "1/(x2+x2)" division expression. Specifically, the framework 
knows that the least significant bit of the "x2+x2" expression must be zero.

The get_nonzero_bits information of the left hand side and right hand side of 
expressions needed to be passed down to operator_div::wi_fold() in the VRP 
framework. The majority of this change involves adding two additional 
parameters to propagate this information. There are future opportunities to use 
the non zero bit information to perform better optimizations in other types of 
expressions.

The changes were tested against x86_64-pc-linux-gnu and all tests in "make -k 
check" passed.

The original approach was to implement a match.pd pattern to support this but 
the pattern wasn't being triggered. More context is available in: 
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77980
So you're going to want to sync with Aldy & Andrew as they're the experts on the Ranger design & implementation.  This hits the Ranger API as well as design questions about how best to tie in the nonzero_bits capabilities.

You might also want to reach out to Roger Sayle.  He's been poking around in a closely related area, though more focused on the bitwise conditional constant propagation rather than Ranger/VRP.  In fact, I just acked a patch of his that looks closely related.

https://gcc.gnu.org/pipermail/gcc-patches/2021-August/577888.html

Jeff

Reply via email to