From patchwork Sat Sep 16 05:49:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 140961 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp1495389vqi; Fri, 15 Sep 2023 22:50:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IELF6cH1ir2BJogTHtcNq4s6B3AyWcyk7uhA3uFVa3xC6DlLxr+/g7ojkP9R1Q7FvdiFc/v X-Received: by 2002:a19:f619:0:b0:503:60f:1c48 with SMTP id x25-20020a19f619000000b00503060f1c48mr259295lfe.19.1694843418551; Fri, 15 Sep 2023 22:50:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694843418; cv=none; d=google.com; s=arc-20160816; b=08z2NgEtWL35ep5ezTNLakfPdzSa1QQt5zjO0nHBu50ik3wd5NeaYzKtOhtT5o4AtA jCWLan/+65bb7z3pbGbPGGw5vtFE6ObPAJM/3afP9F2iz4teBiOTzMwEK/ttUgmFOdG0 RaCZ+OAPpKe7oI3xUfS3SoDCORR3dlY3d8HebkRRFf3jT5PYsf9RBxrxMCFdp5DtyBPs JRhGOruu7D9UgEAWfQ7J75E5ob1lCl+7fqZY4oahrlzPMTCHzKzG7uuVwzF2BCgZbG/n 377MDua5rUWZ2iMzujbZExo5zDiJPT+MHc5BA50vjlFwLbjVvXa4qon7zaYn6DT7mdEx OTHg== 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-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=3IfwpGq8OGnzFMIY27O0LT2Cv2Yvd587lOXdaM4GMUY=; fh=XbcmRug6/SzczusyoNU1I7Lu5oY0AN2u0mWDzKoxdSs=; b=fHt4yxgiALA7Bhtq60ZdgWABhGalHM5aPzbzXiAygzKbWIk/2tr3Eta19nwS7YtVCD ngfV3aPWLxZ+ZaIDYHSQTqKfsOjj+jpQwN830+9tYJksW96YDBBLceQAmL6NWkakQTLL sgxNPhYtL/wdWUJ8qG9cYggEKr9gWYLZnbyKuzM337g9h8XSCzIiZyucLwAryrm981aH j7RqsXgjW8sWPLHQAvIbRZD4GYVQFWiJCBKwUqFjR+vN7L0wR2W9tDuaiPPEuDwkq4df psG5uvqBZnEYDtW0mFb73gQ8FOpAujt8IlM/pR6sngc8gSguq1YMb0iqTg+t1x+vkSJT Q9ag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=UIX6KZ+F; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id b26-20020aa7c6da000000b00523372ace05si4489939eds.530.2023.09.15.22.50.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 22:50:18 -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=UIX6KZ+F; 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 72E273857C41 for ; Sat, 16 Sep 2023 05:50:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 72E273857C41 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1694843414; bh=3IfwpGq8OGnzFMIY27O0LT2Cv2Yvd587lOXdaM4GMUY=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=UIX6KZ+FkbnYDciUiCG2IAeEvPf2orTWzmkUCgGouyn1L4FWMVinnSZEezqvrzGz8 iiYYZiVHJvLsRC+ttv7V+gF8cjTNYp1+gJ1rVlNSoDYQ9CZrqK/sO8C7BfySiFs0Jm oaavOnC9kTauH9aPCrDvwYVBnSyVCCb3xKf9Szt8= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by sourceware.org (Postfix) with ESMTPS id 53A193858D33 for ; Sat, 16 Sep 2023 05:49:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 53A193858D33 Received: from pps.filterd (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 38G5iAoH009542 for ; Fri, 15 Sep 2023 22:49:31 -0700 Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3t4nq6bfc8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Fri, 15 Sep 2023 22:49:31 -0700 Received: from DC5-EXCH02.marvell.com (10.69.176.39) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Fri, 15 Sep 2023 22:49:29 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server id 15.0.1497.48 via Frontend Transport; Fri, 15 Sep 2023 22:49:29 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 0E7083F7040; Fri, 15 Sep 2023 22:49:28 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] MATCH: Add simplifications for `(a * zero_one) ==/!= CST` Date: Fri, 15 Sep 2023 22:49:20 -0700 Message-ID: <20230916054920.1653864-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: 08pusBY5P_TeDdcoWAq5x7EWLOsQYe5j X-Proofpoint-GUID: 08pusBY5P_TeDdcoWAq5x7EWLOsQYe5j X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.601,FMLib:17.11.176.26 definitions=2023-09-15_20,2023-09-15_01,2023-05-22_02 X-Spam-Status: No, score=-14.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Andrew Pinski via Gcc-patches From: Andrew Pinski Reply-To: Andrew Pinski Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777172132414749249 X-GMAIL-MSGID: 1777172132414749249 Transforming `(a * b@[0,1]) != 0` into `((cast)b) & a != 0` will produce better code as a lot of the time b is defined by a comparison. Also since canonicalize `a & -zero_one` into `a * zero_one` we start to lose information when doing comparisons against 0. In the case of PR 110992, we lose that `a != 0` on the branch and then don't do a jump threading like we should. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/110992 gcc/ChangeLog: * match.pd (`a * zero_one !=/== CST`): New pattern. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/vrp116.c: Update test to avoid the extra comparison. * gcc.c-torture/execute/pr110992-1.c: New test. * gcc.dg/tree-ssa/pr110992-1.c: New test. * gcc.dg/tree-ssa/pr110992-2.c: New test. --- gcc/match.pd | 15 +++++++ .../gcc.c-torture/execute/pr110992-1.c | 43 +++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr110992-1.c | 21 +++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr110992-2.c | 17 ++++++++ gcc/testsuite/gcc.dg/tree-ssa/vrp116.c | 2 +- 5 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr110992-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110992-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110992-2.c diff --git a/gcc/match.pd b/gcc/match.pd index 39c9c81966a..97405e6a5c3 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -2197,6 +2197,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (INTEGRAL_TYPE_P (type)) (bit_and @0 @1))) +/* (a * b@[0,1]) == CST + -> + CST == 0 ? (a == CST | b == 0) : (a == CST & b != 0) + (a * b@[0,1]) != CST + -> + CST != 0 ? (a != CST | b == 0) : (a != CST & b != 0) */ +(for cmp (ne eq) + (simplify + (cmp (mult:cs @0 zero_one_valued_p@1) INTEGER_CST@2) + (if ((cmp == EQ_EXPR) ^ (wi::to_wide (@2) != 0)) + (bit_ior + (cmp @0 @2) + (convert (bit_xor @1 { build_one_cst (TREE_TYPE (@1)); }))) + (bit_and (cmp @0 @2) (convert @1))))) + (for cmp (tcc_comparison) icmp (inverted_tcc_comparison) /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1. */ diff --git a/gcc/testsuite/gcc.c-torture/execute/pr110992-1.c b/gcc/testsuite/gcc.c-torture/execute/pr110992-1.c new file mode 100644 index 00000000000..edb7eb75ef2 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/pr110992-1.c @@ -0,0 +1,43 @@ +#define CST 5 +#define OP != +#define op_eq == +#define op_ne != + +#define function(vol,op, cst) \ +__attribute__((noipa)) \ +_Bool func_##op##_##cst##_##vol(vol int a, vol _Bool b) \ +{ \ + vol int d = (a * b); \ + return d op_##op cst; \ +} + +#define funcdefs(op,cst) \ +function(,op,cst) \ +function(volatile,op,cst) + +#define funcs(f) \ +f(eq,0) \ +f(eq,1) \ +f(eq,5) \ +f(ne,0) \ +f(ne,1) \ +f(ne,5) + +funcs(funcdefs) + +#define test(op,cst) \ +do { \ + if(func_##op##_##cst##_(a,b) != func_##op##_##cst##_volatile(a,b))\ + __builtin_abort(); \ +} while(0); + +int main(void) +{ + for(int a = -10; a <= 10; a++) + { + _Bool b = 0; + funcs(test) + b = 1; + funcs(test) + } +} diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110992-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr110992-1.c new file mode 100644 index 00000000000..825fd63f84c --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110992-1.c @@ -0,0 +1,21 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -fdump-tree-optimized" } */ +static unsigned b; +static short c = 4; +void foo(void); +static short(a)(short d, short g) { return d * g; } +void e(); +static char f() { + b = 0; + return 0; +} +int main() { + int h = b; + if ((short)(a(c && e, 65535) & h)) { + foo(); + h || f(); + } +} + +/* There should be no calls to foo left. */ +/* { dg-final { scan-tree-dump-not " foo " "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110992-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr110992-2.c new file mode 100644 index 00000000000..6082949a218 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110992-2.c @@ -0,0 +1,17 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -fdump-tree-optimized" } */ +static unsigned b; +static short c = 4; +void foo(void); +int main() { + int h = b; + int d = c != 0; + if (h*d) { + foo(); + if (!h) b = 20; + } +} + + +/* There should be no calls to foo left. */ +/* { dg-final { scan-tree-dump-not " foo " "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp116.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp116.c index 9e68a774aee..16b31e320a0 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/vrp116.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp116.c @@ -6,7 +6,7 @@ f (int m1, int m2, int c) { int d = m1 > m2; int e = d * c; - return e ? m1 : m2; + return e; } /* { dg-final { scan-tree-dump-times "\\? c_\[0-9\]\\(D\\) : 0" 1 "optimized" } } */