PR tree-optimization/64992: (B << 2) != 0 is B when B is Boolean.
authorRoger Sayle <roger@nextmovesoftware.com>
Mon, 15 Aug 2022 16:39:47 +0000 (17:39 +0100)
committerRoger Sayle <roger@nextmovesoftware.com>
Mon, 15 Aug 2022 16:39:47 +0000 (17:39 +0100)
commit418b71c0d535bf91df78bad2e198c57934682eaa
tree16091b0ba4b39cef0024221c90b938a31b1c0939
parent03acd8b6429e22068330dce5abf129291d3f26de
PR tree-optimization/64992: (B << 2) != 0 is B when B is Boolean.

This patch resolves both PR tree-optimization/64992 and PR
tree-optimization/98956 which are missed optimization enhancement
request, for which Andrew Pinski already has a proposed solution
(related to a fix for PR tree-optimization/98954).  Yesterday,
I proposed an alternate improved patch for PR98954, which although
superior in most respects, alas didn't address this case [which
doesn't include a BIT_AND_EXPR], hence this follow-up fix.

For many functions, F(B), of a (zero-one) Boolean value B, the
expression F(B) != 0 can often be simplified to just B.  Hence
"(B * 5) != 0" is B, "-B != 0" is B, "bswap(B) != 0" is B,
"(B >>r 3) != 0" is B.  These are all currently optimized by GCC,
with the strange exception of left shifts by a constant (possibly
due to the undefined/implementation defined behaviour when the
shift constant is larger than the first operand's precision).
This patch adds support for this particular case, when the shift
constant is valid.

2022-08-15  Roger Sayle  <roger@nextmovesoftware.com>

gcc/ChangeLog
PR tree-optimization/64992
PR tree-optimization/98956
* match.pd (ne (lshift @0 @1) 0): Simplify (X << C) != 0 to X
when X is zero_one_valued_p and the shift constant C is valid.
(eq (lshift @0 @1) 0): Likewise, simplify (X << C) == 0 to !X
when X is zero_one_valued_p and the shift constant C is valid.

gcc/testsuite/ChangeLog
PR tree-optimization/64992
* gcc.dg/pr64992.c: New test case.
gcc/match.pd
gcc/testsuite/gcc.dg/pr64992.c [new file with mode: 0644]