From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id 161233858C54 for ; Mon, 20 Jun 2022 08:18:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 161233858C54 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id DF01F1042; Mon, 20 Jun 2022 01:18:51 -0700 (PDT) Received: from localhost (e121540-lin.manchester.arm.com [10.32.98.37]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 9A4ED3F5A1; Mon, 20 Jun 2022 01:18:50 -0700 (PDT) From: Richard Sandiford To: Richard Biener via Gcc-patches Mail-Followup-To: Richard Biener via Gcc-patches , Tamar Christina , Richard Biener , Richard Guenther , nd , richard.sandiford@arm.com Cc: Tamar Christina , Richard Biener , Richard Guenther , nd Subject: Re: [PATCH 1/2]middle-end: Simplify subtract where both arguments are being bitwise inverted. References: Date: Mon, 20 Jun 2022 09:18:48 +0100 In-Reply-To: (Richard Biener via Gcc-patches's message of "Mon, 20 Jun 2022 10:03:29 +0200") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-57.2 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 20 Jun 2022 08:18:53 -0000 Richard Biener via Gcc-patches writes: > On Thu, Jun 16, 2022 at 1:10 PM Tamar Christina via Gcc-patches > wrote: >> >> Hi All, >> >> 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)a - (int)b; >> } >> >> 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. >> >> Bootstrapped Regtested on aarch64-none-linux-gnu and no issues. >> >> Ok for master? >> >> Thanks, >> Tamar >> >> gcc/ChangeLog: >> >> * match.pd: New bit_not rule. >> >> gcc/testsuite/ChangeLog: >> >> * gcc.dg/subnot.c: New test. >> >> --- inline copy of patch -- >> diff --git a/gcc/match.pd b/gcc/match.pd >> index a59b6778f661cf9121dd3503f43472871e4da445..51b0a1b562409af535e53828a10c30b8a3e1ae2e 100644 >> --- a/gcc/match.pd >> +++ b/gcc/match.pd >> @@ -1258,6 +1258,10 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) >> (simplify >> (bit_not (plus:c (bit_not @0) @1)) >> (minus @0 @1)) >> +/* (~X - ~Y) -> X - Y. */ >> +(simplify >> + (minus (bit_not @0) (bit_not @1)) >> + (minus @0 @1)) > > It doesn't seem correct. > > (gdb) p/x ~-1 - ~0x80000000 > $3 = 0x80000001 > (gdb) p/x -1 - 0x80000000 > $4 = 0x7fffffff > > where I was looking for a case exposing undefined integer overflow. Yeah, shouldn't it be folding to (minus @1 @0) instead? ~X = (-X - 1) -Y = (-Y - 1) so: ~X - ~Y = (-X - 1) - (-Y - 1) = -X - 1 + Y + 1 = Y - X Richard > Richard. > >> >> /* ~(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 0000000000000000000000000000000000000000..d621bacd27bd3d19a010e4c9f831aa77d28bd02d >> --- /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" } } */ >> >> >> >> >> --