From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 9FA0E3955C9E for ; Thu, 2 Jun 2022 09:18:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9FA0E3955C9E Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-638-7pYkSQNCMciyGftyd7LEtQ-1; Thu, 02 Jun 2022 05:18:23 -0400 X-MC-Unique: 7pYkSQNCMciyGftyd7LEtQ-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 02D10185A794; Thu, 2 Jun 2022 09:18:23 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.33.36.77]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BAFE8492C3B; Thu, 2 Jun 2022 09:18:22 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 2529IKj9733880 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 2 Jun 2022 11:18:20 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 2529IJcD733879; Thu, 2 Jun 2022 11:18:19 +0200 Date: Thu, 2 Jun 2022 11:18:19 +0200 From: Jakub Jelinek To: Richard Biener 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] Message-ID: Reply-To: Jakub Jelinek References: MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, 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: Thu, 02 Jun 2022 09:18:28 -0000 On Thu, Jun 02, 2022 at 08:36:42AM +0000, Richard Biener wrote: > > --- 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 ... I saw it used in another pattern, so thought it will work fine: (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0) And looking at the generated source, I think it does work: case CFN_MUL_OVERFLOW: if (gimple_call_num_args (_c1) == 2) { tree _q20 = gimple_call_arg (_c1, 0); _q20 = do_valueize (valueize, _q20); tree _q21 = gimple_call_arg (_c1, 1); _q21 = do_valueize (valueize, _q21); if (integer_nonzerop (_q21)) { { /* #line 5976 "../../gcc/match.pd" */ tree captures[3] ATTRIBUTE_UNUSED = { _p0, _q20, _q21 }; if (gimple_simplify_256 (res_op, seq, valueize, type, captures)) return true; } } if (integer_nonzerop (_q20)) { { /* #line 5976 "../../gcc/match.pd" */ tree captures[3] ATTRIBUTE_UNUSED = { _p0, _q21, _q20 }; if (gimple_simplify_256 (res_op, seq, valueize, type, captures)) return true; } } } Though, sure, I should test it in the testcase too. Here is what I've committed as obvious after testing on x86_64-linux -m32/-m64: 2022-06-02 Jakub Jelinek PR middle-end/30314 * gcc.dg/tree-ssa/pr30314.c: Add tests with swapped arguments. --- gcc/testsuite/gcc.dg/tree-ssa/pr30314.c.jj 2022-06-01 17:54:30.000000000 +0200 +++ gcc/testsuite/gcc.dg/tree-ssa/pr30314.c 2022-06-02 11:01:27.818987766 +0200 @@ -4,6 +4,8 @@ /* { 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 } } } */ +/* { dg-final { scan-tree-dump " > 102261126" "optimized" { target int32 } } } */ +/* { dg-final { scan-tree-dump " > 439208192231179800" "optimized" { target lp64 } } } */ int foo (unsigned int x) @@ -16,3 +18,15 @@ bar (unsigned long int x) { return __builtin_mul_overflow_p (x, 35UL, 0UL); } + +int +baz (unsigned int x) +{ + return __builtin_mul_overflow_p (42, x, 0U); +} + +int +qux (unsigned long int x) +{ + return __builtin_mul_overflow_p (42, x, 0UL); +} Jakub