From patchwork Fri May 19 07:59:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 96232 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1060917vqo; Fri, 19 May 2023 01:00:44 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7wS4+d9ABDKS0x25zoFlTDvJKhEetuF6vgHj8VREUSvIDaWge8cZ/A9T9cHL8ANodcbVet X-Received: by 2002:aa7:d5d7:0:b0:506:8d35:40b7 with SMTP id d23-20020aa7d5d7000000b005068d3540b7mr856126eds.8.1684483243862; Fri, 19 May 2023 01:00:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684483243; cv=none; d=google.com; s=arc-20160816; b=C/FLpigNjxsk4u++SEGbjTj1TECiXSVEtFcPZfzMcdMqUuwVYSG/UkIjLva2Kgqpdk ytx/XHD3SaGd2oNOLoQW08uYi7s7KjJVZbC+Oq5eLTdnpgv9owxgioTsLmdaN9ER1sfW /oi+mGJ1BL+Q9uy6XJdku2kG502nrPUyCCSBxarf+OogGW5RCLX4W0I2q0oEgNeZ/j/1 xkFjmm+VPPaflVyqUZviQ7j3kTKbSZWsuOliEU2q39nM8rgt2tUBW5gGTOiVNraQD9IF h1DxIPvg97Omd2ko0ZOy9laFrv2XBqHNC4i1b/GhVH/LzrAOK8UJJ2Bh7HMENOoPHcLm +E9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=BKGU/Ro4NnCm+1svPdVEHNG/DXkFfvEkzk5lgK5V9+0=; b=UlEghHeD7QWf9H77ffQ96ihruXEGqtXXA1r/jJ60vwKdI/7fJQnxsyXYzD1nuBaccn 4mUO7HG6p0mLQs9XcNiCqULx4e1WKNLuguraq2Ahwd5XJrhjegQUU6QerH7AOX7RhZi+ k8ge7f7eqFsEd1ltjNvFfUgqnGwPsoTULkeiZTp3/s3lJf2/CVLf45JdDn4q4de69q9K 1XS9DCyoP+h6sYtWiKNDQqPdAjWJmSFsRBFH7O8WJZbeaadLCE/1ivYMvztgAzur1PP8 15Kp9YJ5Ws9LivXju/9668ZiIPabf+txdZJumVoK3aCKvEVb1fTYLa3lOzx9rDl5NV5Y vQbg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Qrt6AMTl; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id d12-20020a056402078c00b0050dfe0d8750si2469357edy.632.2023.05.19.01.00.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 May 2023 01:00:43 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Qrt6AMTl; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C32A43858421 for ; Fri, 19 May 2023 08:00:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C32A43858421 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684483242; bh=BKGU/Ro4NnCm+1svPdVEHNG/DXkFfvEkzk5lgK5V9+0=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=Qrt6AMTl96nzvGvrLysm36qJ6KYP+eBLmZdPSavx1cDDl3Sae/v1mlDOlvmKeDxnj 57+TrpoFW8RDm8l7VFEh464X7Rgdo9SMJM0tIcU3QtSte092lHcLl4S9cS2EWvMcst MlH97HFfC55mIPuTyrSuokmTkS2rbkyLqd4JVGyU= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id DA1083858D28 for ; Fri, 19 May 2023 07:59:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DA1083858D28 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-192-DBC2hZktPKuxu3dENKywXA-1; Fri, 19 May 2023 03:59:52 -0400 X-MC-Unique: DBC2hZktPKuxu3dENKywXA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2D32E85A5B5; Fri, 19 May 2023 07:59:52 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.17]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DDE6140D1B60; Fri, 19 May 2023 07:59:51 +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 34J7xniE3569497 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 19 May 2023 09:59:49 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 34J7xmPt3569496; Fri, 19 May 2023 09:59:48 +0200 Date: Fri, 19 May 2023 09:59:47 +0200 To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-ssa-math-opts: Pattern recognize hand written __builtin_mul_overflow_p with same unsigned types even when target just has highpart umul [PR101856] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, 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 Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766308702086154998?= X-GMAIL-MSGID: =?utf-8?q?1766308702086154998?= Hi! As can be seen on the following testcase, we pattern recognize it on i?86/x86_64 as return __builtin_mul_overflow_p (x, y, 0UL) and avoid that way the extra division, but don't do it e.g. on aarch64 or ppc64le, even when return __builtin_mul_overflow_p (x, y, 0UL); actually produces there better code. The reason for testing the presence of the optab handler is to make sure the generated code for it is short to ensure we don't actually pessimize code instead of optimizing it. But, we have one case that the internal-fn.cc .MUL_OVERFLOW expansion handles nicely, and that is when arguments/result is the same mode TYPE_UNSIGNED type, we only use IMAGPART_EXPR of it (i.e. __builtin_mul_overflow_p rather than __builtin_mul_overflow) and umul_highpart_optab supports the particular mode, in that case we emit comparison of the highpart umul result against zero. So, the following patch matches what we do in internal-fn.cc and also pattern matches __builtin_mul_overflow_p if 1) we only need the flag whether it overflowed (i.e. !use_seen) 2) it is unsigned (i.e. !cast_stmt) 3) umul_highpart is supported for the mode Bootstrapped/regtested on x86_64-linux, i686-linux, aarch64-linux and powerpc64le-linux, ok for trunk? 2023-05-19 Jakub Jelinek PR tree-optimization/101856 * tree-ssa-math-opts.cc (match_arith_overflow): Pattern detect unsigned __builtin_mul_overflow_p even when umulv4_optab doesn't support it but umul_highpart_optab does. * gcc.dg/tree-ssa/pr101856.c: New test. Jakub --- gcc/tree-ssa-math-opts.cc.jj 2023-05-17 20:57:59.537914382 +0200 +++ gcc/tree-ssa-math-opts.cc 2023-05-18 12:04:09.332336899 +0200 @@ -4074,7 +4074,10 @@ match_arith_overflow (gimple_stmt_iterat TYPE_MODE (type)) == CODE_FOR_nothing) || (code == MULT_EXPR && optab_handler (cast_stmt ? mulv4_optab : umulv4_optab, - TYPE_MODE (type)) == CODE_FOR_nothing)) + TYPE_MODE (type)) == CODE_FOR_nothing + && (use_seen + || cast_stmt + || !can_mult_highpart_p (TYPE_MODE (type), true)))) { if (code != PLUS_EXPR) return false; --- gcc/testsuite/gcc.dg/tree-ssa/pr101856.c.jj 2023-05-18 11:57:17.681206745 +0200 +++ gcc/testsuite/gcc.dg/tree-ssa/pr101856.c 2023-05-18 11:56:51.662577752 +0200 @@ -0,0 +1,11 @@ +/* PR tree-optimization/101856 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* { dg-final { scan-tree-dump " .MUL_OVERFLOW " "optimized" { target i?86-*-* x86_64-*-* aarch64*-*-* powerpc64le-*-* } } } */ + +int +foo (unsigned long x, unsigned long y) +{ + unsigned long z = x * y; + return z / y != x; +}