From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1251) id CA2EF3858D1E; Mon, 15 Aug 2022 16:40:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CA2EF3858D1E MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Roger Sayle To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-2048] PR tree-optimization/64992: (B << 2) != 0 is B when B is Boolean. X-Act-Checkin: gcc X-Git-Author: Roger Sayle X-Git-Refname: refs/heads/master X-Git-Oldrev: 03acd8b6429e22068330dce5abf129291d3f26de X-Git-Newrev: 418b71c0d535bf91df78bad2e198c57934682eaa Message-Id: <20220815164058.CA2EF3858D1E@sourceware.org> Date: Mon, 15 Aug 2022 16:40:58 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 15 Aug 2022 16:40:58 -0000 https://gcc.gnu.org/g:418b71c0d535bf91df78bad2e198c57934682eaa commit r13-2048-g418b71c0d535bf91df78bad2e198c57934682eaa Author: Roger Sayle Date: Mon Aug 15 17:39:47 2022 +0100 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 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. Diff: --- gcc/match.pd | 20 ++++++++++++++++++++ gcc/testsuite/gcc.dg/pr64992.c | 7 +++++++ 2 files changed, 27 insertions(+) diff --git a/gcc/match.pd b/gcc/match.pd index e7d10f427a9..e32bda64e64 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1920,6 +1920,26 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))) (mult (convert @1) (convert @2)))) +/* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p. + Check that the shift is well-defined (C is less than TYPE_PRECISION) + as some targets (such as x86's SSE) may return zero for larger C. */ +(simplify + (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2) + (if (tree_fits_shwi_p (@1) + && tree_to_shwi (@1) > 0 + && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0))) + (convert @0))) + +/* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p. + Check that the shift is well-defined (C is less than TYPE_PRECISION) + as some targets (such as x86's SSE) may return zero for larger C. */ +(simplify + (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2) + (if (tree_fits_shwi_p (@1) + && tree_to_shwi (@1) > 0 + && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0))) + (eq @0 @2))) + /* Convert ~ (-A) to A - 1. */ (simplify (bit_not (convert? (negate @0))) diff --git a/gcc/testsuite/gcc.dg/pr64992.c b/gcc/testsuite/gcc.dg/pr64992.c new file mode 100644 index 00000000000..43fbcf7ac50 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr64992.c @@ -0,0 +1,7 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +_Bool foo(_Bool x) { return (x << 2) != 0; } +_Bool bar(_Bool x) { return (x << 2) == 0; } + +/* { dg-final { scan-tree-dump-not " << " "optimized" } } */