From patchwork Wed Jun 1 13:55:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1637979 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=qJvs1u7G; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LCrKM51zsz9s0w for ; Wed, 1 Jun 2022 23:56:22 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 69645382FE6A for ; Wed, 1 Jun 2022 13:56:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 69645382FE6A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1654091779; bh=AviqEb8UgQ5oXsIXcib4ztsYgPn9zuqKUonvUomFr24=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=qJvs1u7GPRRBGCh1GBBkRyCGvQWQqgnEedy4H8lEsArLLTc3lUDsNhTgYCdFSmqgu Pyd8/BZcxUmjLG4TT67ZcZCIAj5X268vvZw5pDirkLLXrzTJ3k37lXWhJWP6POrgty xpOIrNHNVZgLswPCvP4b7tyyFoRUUc6D8rWKb11I= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 8FD72385ED4D for ; Wed, 1 Jun 2022 13:55:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8FD72385ED4D 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-237-fK-J7_tIPgS3TPHEl5swhA-1; Wed, 01 Jun 2022 09:55:54 -0400 X-MC-Unique: fK-J7_tIPgS3TPHEl5swhA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8873B802804; Wed, 1 Jun 2022 13:55:54 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.33.36.77]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4D864C23DC1; Wed, 1 Jun 2022 13:55:54 +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 251DtptO3107284 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 1 Jun 2022 15:55:52 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 251Dtobk3107283; Wed, 1 Jun 2022 15:55:50 +0200 Date: Wed, 1 Jun 2022 15:55:50 +0200 To: Richard Biener Subject: [PATCH] match.pd: Optimize __builtin_mul_overflow_p (x, cst, (utype)0) to x > ~(utype)0 / cst [PR30314] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 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. Jakub --- 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)) + (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); +}