From patchwork Sun May 7 22:19:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 90887 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1783032vqo; Sun, 7 May 2023 15:21:06 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ58qADanXJMW1sT8ZED2xU0uLGB46uR7GihUEu4oZfrXq4gX1NTH9CCn4EagGqQb4GYFFQt X-Received: by 2002:a17:907:3683:b0:966:dd1:bae2 with SMTP id bi3-20020a170907368300b009660dd1bae2mr5217666ejc.73.1683498066033; Sun, 07 May 2023 15:21:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683498066; cv=none; d=google.com; s=arc-20160816; b=GVsmxisDzOho3YIQ+UQBxV3noBKfgHHE/5rWAS9DQv/v2nJeVDpyPtKmGRyO+xNnW9 Vo4Ymwm3nfZHXRBkkDQ2Fb+0yilPT+YVdpxNt8zD4Rdxdi0n2peE26FdTg/pR9L1Lu7W vd1GhHvwZqA+YPhVz/rzQqex6vKTN4G3Y7j1vIy+Nd7MTnJIFd86fHWcLAktEkaqDxLG 8Ifr20YK0a/HMnjzrWhF/mVmfQz5rLfKI1j2VZFRFOnFCH8zG3bX9PmjuMNPq7MQqsfU LE4UJFEGy8ZltA2uch9noUje3cYDXoslUYNq8EvQHSVffAL1XolfysxABOIgj3zRgIEA ff3w== 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=QKyuIs90aBUcDoXHY3qLKwszaD/qFYVxDgwE+G+/sAg=; b=QN6h5VnFIaj/4PUB8kBvw9FbVtenwiJWAUtR0BDDBlM+e+/sbLsQK8LW/4A5sdGOM2 Pe6AUsFcrGh/hT0soJmeWfmWIvyDAEazvjeJxXrqJondBHlWZz3ces+md37n0GlJyi+s V2EVeHaiQl8atZ6eQ4U7eOMxh6IG4SPvumVLMmYtgcnhvJf8Eg/cRMfrcyDND3NlA4gY VszS7DxmaSlOhxuwYWrspq1Ofd57Ilf3USobMNwo+FZCdopEluftd1FleI+ZIHgS9bA+ bShkIkd6Hq/YSWR1bQGObdFloDQiWDtrSYYOMoTZYj6W915QZTS0QKcKsK0XYy1nNZF5 buSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=m5ST82XJ; 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 jt18-20020a170906dfd200b0092feaa573ffsi5255211ejc.632.2023.05.07.15.21.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 May 2023 15:21:06 -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=m5ST82XJ; 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 CEE83385828D for ; Sun, 7 May 2023 22:21:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CEE83385828D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1683498064; bh=QKyuIs90aBUcDoXHY3qLKwszaD/qFYVxDgwE+G+/sAg=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=m5ST82XJ8ZU4iDjK51KMUkk2QgDlakUdmDDUU3lOzorIba83d42wntb1HSvmKOowN YMPknay8JmgjjOTORLDVjkPVuX2eQDX3GlpJCzJIYE4hp8t7OdA960wJPU3DwAl81y jyFl/3pZk65O1St9PO4S6KznRCjvr6zIT6jF0zSc= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0016f401.pphosted.com (mx0a-0016f401.pphosted.com [67.231.148.174]) by sourceware.org (Postfix) with ESMTPS id 8E47A3858D32 for ; Sun, 7 May 2023 22:20:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8E47A3858D32 Received: from pps.filterd (m0045849.ppops.net [127.0.0.1]) by mx0a-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 347LtIce013245 for ; Sun, 7 May 2023 15:20:16 -0700 Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3qdm8t3v54-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Sun, 07 May 2023 15:20:15 -0700 Received: from DC5-EXCH01.marvell.com (10.69.176.38) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Sun, 7 May 2023 15:20:08 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server id 15.0.1497.48 via Frontend Transport; Sun, 7 May 2023 15:20:08 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 5432D5B692B; Sun, 7 May 2023 15:20:07 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] MATCH: Move `a <= CST1 ? MAX : a` optimization to match Date: Sun, 7 May 2023 15:19:59 -0700 Message-ID: <20230507221959.1166993-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-GUID: SvgEg_mbxWnl9FlWIoAno8UXccZ5jhdz X-Proofpoint-ORIG-GUID: SvgEg_mbxWnl9FlWIoAno8UXccZ5jhdz X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-05-07_08,2023-05-05_01,2023-02-09_01 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_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: 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765275668093160104?= X-GMAIL-MSGID: =?utf-8?q?1765275668093160104?= This moves the `a <= CST1 ? MAX : a` optimization from phiopt to match. It just adds a new pattern to match.pd. There is one more change needed before being able to remove minmax_replacement from phiopt. A few notes on the testsuite changes: * phi-opt-5.c is now able to optimize at phiopt1 so remove the xfail. * pr66726-4.c can be optimized during fold before phiopt1 so need to change the scanning. * pr66726-5.c needs two phiopt passes currently to optimize to the right thing, it needed 2 phiopt passes before, the cast from int to unsigned char is the reason. * pr66726-6.c is what the original pr66726-4.c was testing before the fold was able to optimize it. OK? Bootstrapped and tested on x86_64-linux-gnu. gcc/ChangeLog: * match.pd (`(a CMP CST1) ? max : a`): New pattern. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/phi-opt-5.c: Remove last xfail. * gcc.dg/tree-ssa/pr66726-4.c: Change how scanning works. * gcc.dg/tree-ssa/pr66726-5.c: New test. * gcc.dg/tree-ssa/pr66726-6.c: New test. --- gcc/match.pd | 18 +++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/phi-opt-5.c | 2 +- gcc/testsuite/gcc.dg/tree-ssa/pr66726-4.c | 5 +++- gcc/testsuite/gcc.dg/tree-ssa/pr66726-5.c | 28 +++++++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr66726-6.c | 17 ++++++++++++++ 5 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr66726-5.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr66726-6.c diff --git a/gcc/match.pd b/gcc/match.pd index ceae1c34abc..a55ede838cd 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4954,6 +4954,24 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (code == MAX_EXPR) (minmax (max @1 @2) @4))))))) +/* Optimize (a CMP CST1) ? max : a */ +(for cmp (gt ge lt le) + minmax (min min max max) + (simplify + (cond (cmp @0 @1) (minmax:c@2 @0 @3) @4) + (with + { + tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4); + } + (if ((cmp == LT_EXPR || cmp == LE_EXPR) + && code == MIN_EXPR + && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @1))) + (min @2 @4) + (if ((cmp == GT_EXPR || cmp == GE_EXPR) + && code == MAX_EXPR + && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @1))) + (max @2 @4)))))) + /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2. */ (simplify (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-5.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-5.c index 5f78a1ba6dc..e78d9d8b83d 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-5.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-5.c @@ -39,7 +39,7 @@ float repl2 (float vary) /* phiopt1 confused by predictors. */ /* { dg-final { scan-tree-dump "vary.*MAX_EXPR.*0\\.0" "phiopt1" } } */ -/* { dg-final { scan-tree-dump "vary.*MIN_EXPR.*1\\.0" "phiopt1" { xfail *-*-* } } } */ +/* { dg-final { scan-tree-dump "vary.*MIN_EXPR.*1\\.0" "phiopt1" } } */ /* { dg-final { scan-tree-dump "vary.*MAX_EXPR.*0\\.0" "phiopt2"} } */ /* { dg-final { scan-tree-dump "vary.*MIN_EXPR.*1\\.0" "phiopt2"} } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr66726-4.c b/gcc/testsuite/gcc.dg/tree-ssa/pr66726-4.c index 4e43522f3a3..930ad5fb79f 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/pr66726-4.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr66726-4.c @@ -9,4 +9,7 @@ foo (unsigned char *p, int i) *p = SAT (i); } -/* { dg-final { scan-tree-dump-times "COND_EXPR .*and PHI .*converted to straightline code" 1 "phiopt1" } } */ +/* fold could optimize SAT before phiopt1 so only match on the + MIN/MAX here. */ +/* { dg-final { scan-tree-dump-times "= MIN_EXPR" 1 "phiopt1" } } */ +/* { dg-final { scan-tree-dump-times "= MAX_EXPR" 1 "phiopt1" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr66726-5.c b/gcc/testsuite/gcc.dg/tree-ssa/pr66726-5.c new file mode 100644 index 00000000000..4b5066cdb6b --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr66726-5.c @@ -0,0 +1,28 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-phiopt1-details -fdump-tree-phiopt2-details -fdump-tree-optimized" } */ + +#define SAT(x) (x < 0 ? 0 : (x > 255 ? 255 : x)) + +unsigned char +foo (unsigned char *p, int i) +{ + if (i < 0) + return 0; + { + int t; + if (i > 255) + t = 255; + else + t = i; + return t; + } +} + +/* Because of the way PHIOPT works, it only does the merging of BBs after it is done so we get the case were we can't + optimize the above until phiopt2 right now. */ +/* { dg-final { scan-tree-dump-times "COND_EXPR .*and PHI .*converted to straightline code" 2 "phiopt1" { xfail *-*-* } } } */ +/* { dg-final { scan-tree-dump-times "COND_EXPR .*and PHI .*converted to straightline code" 0 "phiopt2" { xfail *-*-* } } } */ +/* { dg-final { scan-tree-dump-times "= MIN_EXPR" 1 "phiopt1" } } */ +/* { dg-final { scan-tree-dump-times "= MAX_EXPR" 1 "phiopt1" { xfail *-*-* } } } */ +/* { dg-final { scan-tree-dump-times "= MIN_EXPR" 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "= MAX_EXPR" 1 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr66726-6.c b/gcc/testsuite/gcc.dg/tree-ssa/pr66726-6.c new file mode 100644 index 00000000000..5c6b4992608 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr66726-6.c @@ -0,0 +1,17 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-phiopt1-details" } */ + + +unsigned char +foo1 (unsigned char *p, int i) +{ + if (i < 0) + return 0; + { + int t = i > 255 ? 255 : i; + return t; + } +} +/* testing to see if moving the cast out of the conditional. */ + +/* { dg-final { scan-tree-dump-times "COND_EXPR .*and PHI .*converted to straightline code" 1 "phiopt1" } } */