From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1984) id 8E1F13858407; Thu, 4 Aug 2022 15:38:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8E1F13858407 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Tamar Christina To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-1964] middle-end: Simplify subtract where both arguments are being bitwise inverted. X-Act-Checkin: gcc X-Git-Author: Tamar Christina X-Git-Refname: refs/heads/master X-Git-Oldrev: c832ec4c3ec4853ad89ff3b0dbf6e9454e75e8cc X-Git-Newrev: be58bf98e98bb431ed26ca8be84586075fe8be82 Message-Id: <20220804153835.8E1F13858407@sourceware.org> Date: Thu, 4 Aug 2022 15:38:35 +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: Thu, 04 Aug 2022 15:38:35 -0000 https://gcc.gnu.org/g:be58bf98e98bb431ed26ca8be84586075fe8be82 commit r13-1964-gbe58bf98e98bb431ed26ca8be84586075fe8be82 Author: Tamar Christina Date: Thu Aug 4 16:37:25 2022 +0100 middle-end: Simplify subtract where both arguments are being bitwise inverted. This adds a match.pd rule that drops the bitwwise nots when both arguments to a subtract is inverted. i.e. for: float g(float a, float b) { return ~(int)a - ~(int)b; } we instead generate float g(float a, float b) { return (int)b - (int)a; } We already do a limited version of this from the fold_binary fold functions but this makes a more general version in match.pd that applies more often. gcc/ChangeLog: * match.pd: New bit_not rule. gcc/testsuite/ChangeLog: * gcc.dg/subnot.c: New test. Diff: --- gcc/match.pd | 5 +++++ gcc/testsuite/gcc.dg/subnot.c | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/gcc/match.pd b/gcc/match.pd index d3d73e3f55c..f82f94ad1fe 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1308,6 +1308,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (simplify (bit_not (plus:c (bit_not @0) @1)) (minus @0 @1)) +/* (~X - ~Y) -> Y - X. */ +(simplify + (minus (bit_not @0) (bit_not @1)) + (with { tree utype = unsigned_type_for (type); } + (convert (minus (convert:utype @1) (convert:utype @0))))) /* ~(X - Y) -> ~X + Y. */ (simplify diff --git a/gcc/testsuite/gcc.dg/subnot.c b/gcc/testsuite/gcc.dg/subnot.c new file mode 100644 index 00000000000..d621bacd27b --- /dev/null +++ b/gcc/testsuite/gcc.dg/subnot.c @@ -0,0 +1,9 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-optimized" } */ + +float g(float a, float b) +{ + return ~(int)a - ~(int)b; +} + +/* { dg-final { scan-tree-dump-not "~" "optimized" } } */