From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id 0FB2F3857350 for ; Thu, 2 Jun 2022 08:36:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0FB2F3857350 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 0A26E21AF9; Thu, 2 Jun 2022 08:36:43 +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 041B52C141; Thu, 2 Jun 2022 08:36:43 +0000 (UTC) Date: Thu, 2 Jun 2022 08:36:42 +0000 (UTC) From: Richard Biener To: Jakub Jelinek cc: gcc-patches@gcc.gnu.org Subject: Re: [PATCH] match.pd: Optimize __builtin_mul_overflow_p (x, cst, (utype)0) to x > ~(utype)0 / cst [PR30314] 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=-4.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, SPF_HELO_NONE, SPF_PASS, 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: Thu, 02 Jun 2022 08:36:45 -0000 On Wed, 1 Jun 2022, Jakub Jelinek wrote: > Hi! > > A comparison with a constant is most likely always faster than > .MUL_OVERFLOW from which we only check whether it overflowed and not the > multiplication result, and even if not, it is simpler operation on GIMPLE > and even if a target exists where such multiplications with overflow checking > are cheaper than comparisons, because comparisons are so much more common > than overflow checking multiplications, it would be nice if it simply > arranged for comparisons to be emitted like those multiplications on its > own... > > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? > > 2022-06-01 Jakub Jelinek > > PR middle-end/30314 > * match.pd (__builtin_mul_overflow_p (x, cst, (utype) 0) -> > x > ~(utype)0 / cst): New simplification. > > * gcc.dg/tree-ssa/pr30314.c: New test. > > --- gcc/match.pd.jj 2022-06-01 13:54:32.000654151 +0200 > +++ gcc/match.pd 2022-06-01 15:13:35.473084402 +0200 > @@ -5969,6 +5969,17 @@ (define_operator_list SYNC_FETCH_AND_AND > && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0)))) > (ovf @1 @0)))) > > +/* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types > + are unsigned to x > (umax / cst). */ > +(simplify > + (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1)) does :c work here? I think it is at least ignored, possibly diagnostic in genmatch is missing ... > + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) > + && TYPE_UNSIGNED (TREE_TYPE (@0)) > + && TYPE_MAX_VALUE (TREE_TYPE (@0)) > + && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2))) > + && int_fits_type_p (@1, TREE_TYPE (@0))) > + (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1))))) > + > /* Simplification of math builtins. These rules must all be optimizations > as well as IL simplifications. If there is a possibility that the new > form could be a pessimization, the rule should go in the canonicalization > --- gcc/testsuite/gcc.dg/tree-ssa/pr30314.c.jj 2022-06-01 15:22:53.201271365 +0200 > +++ gcc/testsuite/gcc.dg/tree-ssa/pr30314.c 2022-06-01 15:13:24.725196482 +0200 > @@ -0,0 +1,18 @@ > +/* PR middle-end/30314 */ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-optimized" } */ > +/* { dg-final { scan-tree-dump-not "\.MUL_OVERFLOW " "optimized" } } */ > +/* { dg-final { scan-tree-dump " > 122713351" "optimized" { target int32 } } } */ > +/* { dg-final { scan-tree-dump " > 527049830677415760" "optimized" { target lp64 } } } */ > + > +int > +foo (unsigned int x) > +{ > + return __builtin_mul_overflow_p (x, 35U, 0U); > +} > + > +int > +bar (unsigned long int x) > +{ > + return __builtin_mul_overflow_p (x, 35UL, 0UL); > +} > > Jakub > > -- Richard Biener SUSE Software Solutions Germany GmbH, Maxfeldstrasse 5, 90409 Nuernberg, Germany; GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman; HRB 36809 (AG Nuernberg)