From patchwork Tue Jan 3 13:17:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 38427 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp4612775wrt; Tue, 3 Jan 2023 05:17:35 -0800 (PST) X-Google-Smtp-Source: AMrXdXsgTFpR5ev2ggD24tcjIWay/LKusbd7sLOcD4DnF7sDHE/okjXzEL2bNjeUFfKSendB6Ets X-Received: by 2002:a17:907:2c6c:b0:828:7581:f01f with SMTP id ib12-20020a1709072c6c00b008287581f01fmr33518461ejc.23.1672751855022; Tue, 03 Jan 2023 05:17:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672751855; cv=none; d=google.com; s=arc-20160816; b=f6pkB04d2K9At4L8tTymUfKdYfGIZ9XOuXsRmvm9EVtzHY93LwTk3lu6Jhm6TSgH+g L3SYYsKrTvfQhT8/diSIXObKstjb0vdAiflk8wYH7Q1OLc+am6CUB3t5YUX2c25NWjUt E2KEWBgt/xWHpwyRziNhDjwlMxp1OEA7fA7zai2akw8bwY170AMh6uJlJrpfSFou8ptB Fn5LhcmgTUDSrlSNdJxHg1hipEMA9/p5qL621ywfay0be+YwIFPT8lRNm9vMZYuq6NMp bnKW7DyIUdKFB6w8L/0u8iRYav0opihFFRWjFRpwxGvsoUnMcCkR00RpLUSZelADReXZ d0ZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:thread-index:content-language :mime-version:message-id:date:subject:cc:to:from:dkim-signature :delivered-to; bh=p32GStGXlM6sE5WMoIg7PkL7MIpo+NTDI5FOctddCUk=; b=MurPzO6cmgt4edKPZgawtuItZigbobAs+PbKd3fcF/bZbsTc27YBXeN1LFLXSi/7Fi vXS9+1xGSoaIOS3W57gmocH9mVd+DCWMTZC82l/uNkrjD46FjmImIHP21hguNQSwcrD0 JHJPE/rBExZMkg3hL1CaeKz1nNt/9kFfEbJVaDEBzDtSjKwxktRkBl6r4NpBJsrGF0io BO8BH44NUeX/3SlqystBZX4tjtSPkMTO3LW8srgQURHi2a5ZsK+O1sibFKgxfqH3GS0H Me33GrBnAOmBdLAKkdRU2EvOSkoqYmWkSZGa5PAycc8GgnzLlo/cLqBJYdSxBwxuaZ8K F7sg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@nextmovesoftware.com header.s=default header.b=LZNsCzYX; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id wv1-20020a170907080100b007c122a9ba98si29554928ejb.685.2023.01.03.05.17.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Jan 2023 05:17:35 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=fail header.i=@nextmovesoftware.com header.s=default header.b=LZNsCzYX; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1F4E7385800C for ; Tue, 3 Jan 2023 13:17:30 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [162.254.253.69]) by sourceware.org (Postfix) with ESMTPS id CCAB43858D1E for ; Tue, 3 Jan 2023 13:17:04 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=nextmovesoftware.com; s=default; h=Content-Type:MIME-Version:Message-ID: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=p32GStGXlM6sE5WMoIg7PkL7MIpo+NTDI5FOctddCUk=; b=LZNsCzYXjHJ+/khDoVikn2SPKx so2xlUTQDTi87664BqX0rlRtSxLepzvQcc7Uv3jlKRjMOIxWJ3/HOG+Ol/cBHAJvyxQlLKu4ZuUK+ 2C8v3Gf5WdMPLRULIwcLlKvCUzLeBrydDZR2SdfFcnYr2FkImURjbusDo99gkY4NXKv6rWpYHpsrl Up/LxSMrMEZSqfokVAOZaAqkfdDoHsou7Q7hXjwj1Anc+YjD02ML/9t6vUSvfOnFxbdBsyCG8pWzO PhUIbB/RPVDmNEhgK2MJ99jSrZpXYJgBGPmIucOeWS0WZnYJgkYt/e+y3BnPVLBgzwyJ+ZKCJvJ5T oxQ2r+8g==; Received: from [185.62.158.67] (port=63948 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1pChA8-0002j0-24; Tue, 03 Jan 2023 08:17:04 -0500 From: "Roger Sayle" To: "'GCC Patches'" , "'Andrew Pinski'" Cc: "'Richard Biener'" Subject: [PATCH] PR tree-optimization/92342: Optimize b & -(a==c) in match.pd Date: Tue, 3 Jan 2023 13:17:02 -0000 Message-ID: <009101d91f75$ac187920$04496b60$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Content-Language: en-gb Thread-Index: AdkfdOGNFPhRdOOjSBC+iQd8M4hkcA== X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server.nextmovesoftware.com X-AntiAbuse: Original Domain - gcc.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - nextmovesoftware.com X-Get-Message-Sender-Via: server.nextmovesoftware.com: authenticated_id: roger@nextmovesoftware.com X-Authenticated-Sender: server.nextmovesoftware.com: roger@nextmovesoftware.com X-Source: X-Source-Args: X-Source-Dir: X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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: , 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?1754007448840834257?= X-GMAIL-MSGID: =?utf-8?q?1754007448840834257?= This patch is an update/tweak of Andrew Pinski's two patches for PR tree-optimization/92342, that were originally posted back in November: https://gcc.gnu.org/pipermail/gcc-patches/2021-November/585111.html https://gcc.gnu.org/pipermail/gcc-patches/2021-November/585112.html Technically, the first of those was approved by Richard Biener, though never committed, and my first thought was to simply push it for Andrew, but the review of the second piece expressed concerns over comparisons in non-integral modes, where the result may not be zero-one valued. Indeed both transformations misbehave in the presence of vector mode comparisons (these transformations are already implemented for vec_cond elsewhere in match.pd), so my minor contribution is to limit these new transformations to scalars, by testing that both the operands and results are INTEGRAL_TYPE_P. This patch has been tested on x86_64-pc-linux-gnu with make bootstrap and make -k check, both with and without --target_board=unix{-m32}, with no new failures. Ok for mainline? 2023-01-03 Andrew Pinski Roger Sayle gcc/ChangeLog: PR tree-optimization/92342 * match.pd ((m1 CMP m2) * d -> (m1 CMP m2) ? d : 0): Use tcc_comparison and :c for the multiply. (b & -(a CMP c) -> (a CMP c)?b:0): New pattern. gcc/testsuite/ChangeLog: PR tree-optimization/92342 * gcc.dg/tree-ssa/andnegcmp-1.c: New test. * gcc.dg/tree-ssa/andnegcmp-2.c: New test. * gcc.dg/tree-ssa/multcmp-1.c: New test. * gcc.dg/tree-ssa/multcmp-1.c: New test. Thanks in advance (and thanks to Andrew). Roger diff --git a/gcc/match.pd b/gcc/match.pd index 697d8de..2e1f113 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -2076,10 +2076,20 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */ (if (!canonicalize_math_p ()) - (for cmp (gt lt ge le) + (for cmp (tcc_comparison) (simplify - (mult (convert (cmp @0 @1)) @2) - (cond (cmp @0 @1) @2 { build_zero_cst (type); })))) + (mult:c (convert (cmp@0 @1 @2)) @3) + (if (INTEGRAL_TYPE_P (type) + && INTEGRAL_TYPE_P (TREE_TYPE (@0))) + (cond @0 @3 { build_zero_cst (type); }))) +/* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0 */ + (simplify + (bit_and:c (negate (convert (cmp@0 @1 @2))) @3) + (if (INTEGRAL_TYPE_P (type) + && INTEGRAL_TYPE_P (TREE_TYPE (@0))) + (cond @0 @3 { build_zero_cst (type); }))) + ) +) /* For integral types with undefined overflow and C != 0 fold x * C EQ/NE y * C into x EQ/NE y. */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-1.c b/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-1.c new file mode 100644 index 0000000..6f16783 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-1.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* PR tree-optimization/92342 */ + +int +f (int m1, int m2, int c) +{ + int d = m1 == m2; + d = -d; + int e = d & c; + return e; +} + +/* { dg-final { scan-tree-dump-times "\\? c_\[0-9\]\\(D\\) : 0" 1 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-2.c b/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-2.c new file mode 100644 index 0000000..0e25c8a --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-2.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* PR tree-optimization/92342 */ + +int +f (int m1, int m2, int c) +{ + int d = m1 < m2; + d = -d; + int e = c & d; + return e; +} + +/* { dg-final { scan-tree-dump-times "\\? c_\[0-9\]\\(D\\) : 0" 1 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/multcmp-1.c b/gcc/testsuite/gcc.dg/tree-ssa/multcmp-1.c new file mode 100644 index 0000000..fb44cac --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/multcmp-1.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +int +f (int m1, int m2, int c) +{ + int d = m1 == m2; + int e = d * c; + return e; +} + +/* { dg-final { scan-tree-dump-times "\\? c_\[0-9\]\\(D\\) : 0" 1 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/multcmp-2.c b/gcc/testsuite/gcc.dg/tree-ssa/multcmp-2.c new file mode 100644 index 0000000..be38b2e --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/multcmp-2.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +int +f (int m1, int m2, int c) +{ + int d = m1 != m2; + int e = c * d; + return e; +} + +/* { dg-final { scan-tree-dump-times "\\? c_\[0-9\]\\(D\\) : 0" 1 "optimized" } } */