From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id A872C3858C00 for ; Thu, 4 Aug 2022 06:58:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A872C3858C00 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 8C0EB20726; Thu, 4 Aug 2022 06:58:06 +0000 (UTC) Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 6E6A02C141; Thu, 4 Aug 2022 06:58:06 +0000 (UTC) Date: Thu, 4 Aug 2022 06:58:06 +0000 (UTC) From: Richard Biener To: Tamar Christina cc: Richard Biener , Richard Sandiford , Richard Biener via Gcc-patches , nd Subject: RE: [PATCH 1/2]middle-end: Simplify subtract where both arguments are being bitwise inverted. In-Reply-To: Message-ID: References: User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_LOTSOFHASH, SPF_HELO_NONE, SPF_PASS, TXREP 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: Thu, 04 Aug 2022 06:58:13 -0000 On Wed, 3 Aug 2022, Tamar Christina wrote: > > -----Original Message----- > > From: Richard Biener > > Sent: Tuesday, June 21, 2022 8:43 AM > > To: Tamar Christina > > Cc: Richard Sandiford ; Richard Biener via Gcc- > > patches ; Richard Guenther > > ; nd > > Subject: Re: [PATCH 1/2]middle-end: Simplify subtract where both > > arguments are being bitwise inverted. > > > > On Mon, Jun 20, 2022 at 10:49 AM Tamar Christina > > wrote: > > > > > > > -----Original Message----- > > > > From: Richard Sandiford > > > > Sent: Monday, June 20, 2022 9:19 AM > > > > To: Richard Biener via Gcc-patches > > > > Cc: Tamar Christina ; Richard Biener > > > > ; Richard Guenther > > ; > > > > nd > > > > Subject: Re: [PATCH 1/2]middle-end: Simplify subtract where both > > > > arguments are being bitwise inverted. > > > > > > > > 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..51b0a1b562409af535e53828a1 > > > > 0 > > > > >> c30b8a3e1ae2e 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 > > > > > > > > > > You're right, sorry, I should have paid more attention when I wrote the > > patch. > > > > You still need to watch out for undefined overflow cases in the result that > > were well-defined in the original expression I think. > > The only special thing we do for signed numbers if to do the subtract as unsigned. As I mentioned > before GCC already does this transformation as part of the fold machinery, but that only only happens > when a very simple tree is matched and only when single use. i.e. https://godbolt.org/z/EWsdhfrKj > > I'm only attempting to make it apply more generally as the result is always beneficial. > > I've respun the patch to the same as we already do. > > Bootstrapped Regtested on aarch64-none-linux-gnu and no issues. > > Ok for master? OK. > 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 330c1db0c8e12b0fb010b1958729444672403866..00b3e07b2a5216b19ed58500923680d83c67d8cf 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 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" } } */ > -- Richard Biener SUSE Software Solutions Germany GmbH, Frankenstrasse 146, 90461 Nuernberg, Germany; GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman; HRB 36809 (AG Nuernberg)