From patchwork Fri Aug 25 18:33:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 136932 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a7d1:0:b0:3f2:4152:657d with SMTP id p17csp1985104vqm; Fri, 25 Aug 2023 11:34:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF2SeHm1/4s7XvBmx5u3HzK7c5bdxT184VtUeq2whFVHqhMnYS7dKWzhRLYsHtN1SWq7ky9 X-Received: by 2002:aa7:da53:0:b0:523:b37e:b83c with SMTP id w19-20020aa7da53000000b00523b37eb83cmr15888896eds.37.1692988451444; Fri, 25 Aug 2023 11:34:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692988451; cv=none; d=google.com; s=arc-20160816; b=jKonCDXUdSwbbNlC0mXsqNIOH1fx6O+q4Ak+rAlAC/QHYrUxwyV397nTi5R+PnaZpq yBUjsvw5gApTUhhqoqMgRIimm3Cp5HoV+DBzjv1M8EkBh0pzKJ+xxgFV4Z4aEz550H7p YpHGKxnST4ZlvzXD2A+1R2J0oojXBBcEmBnvM/NNAp9WjH9PgZWLrtP6CzjUkmoTLp4/ GhxCXPNRUeQkA0mnBWrvKYXPFm1CRzUoa9o7307fmFwT12bPYQXZFkpQ+fWvksuJd+QZ rfwEZWZaanPl7MSranBJnB1xVfzAWSl/QTINr8K77ZK3kRlz2wzPCXZ62LG01aR/PLZY oTHA== 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=U/mmpPQtGCZjJodIUZsON8LEoBfXpd6Ptnw1o7bS6Tg=; fh=XbcmRug6/SzczusyoNU1I7Lu5oY0AN2u0mWDzKoxdSs=; b=vDDO8b58rvaiZBSgVmlUota4ig0lpm5m1bXVodnZHQjFqzaDBfU7afnBYCh4Pzar8z 00SMFLBtQa6k6TxO9d7WthBS1y+V9i9qrIxj/KpwO7I2rTY2oiZSVLjE10ew2qGHFkjF vaW976vu/mAQEDJWVJvNOwo2CoLkiKW4X0ne/vD01izP0M5xonijOKtWYq+ZEpzrTeXh rjFLKCjnLuaDF/ihnLwuliK6oMrUJV0yGM5B8edND1Vo4YKR4HMf3ZrL3lPCp7JYbyeC HsyFIebHq+fLOpRDnm4im0tiHGgZoyU7Yela90O3hMonnlxpikV/AO0P6sTh0hSUOLv9 cRLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=wFFFDBPU; 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 f15-20020a50ee8f000000b005257f2c057esi1397485edr.127.2023.08.25.11.34.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Aug 2023 11:34:11 -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=wFFFDBPU; 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 5D30A3858421 for ; Fri, 25 Aug 2023 18:34:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5D30A3858421 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692988450; bh=U/mmpPQtGCZjJodIUZsON8LEoBfXpd6Ptnw1o7bS6Tg=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=wFFFDBPUFdy7g3imwp8SZDNuleOkpSuAi3rBVFuqFJ+I7NC7B5Zk5ukUs2YNpNvod E12hbS9sj/cLnGgQhmBMBSwVAQcNt3p+gkYHiqgW2cRdUs1+R7WgYotRi1D2GqRCm2 fmF4Ewn4wkO7D9Vp9jxpEuLlp/t6zuiFriREqgro= 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 274EB3858D32 for ; Fri, 25 Aug 2023 18:33:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 274EB3858D32 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 37PIEBfE005238 for ; Fri, 25 Aug 2023 11:33:24 -0700 Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3spmk2af1y-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Fri, 25 Aug 2023 11:33:24 -0700 Received: from DC5-EXCH02.marvell.com (10.69.176.39) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Fri, 25 Aug 2023 11:33:22 -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, 25 Aug 2023 11:33:22 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 560473F70C6; Fri, 25 Aug 2023 11:33:21 -0700 (PDT) To: CC: Andrew Pinski Subject: [COMMITTEDv2] MATCH: Move `a ? one_zero : one_zero` matching after min/max matching Date: Fri, 25 Aug 2023 11:33:12 -0700 Message-ID: <20230825183312.3604580-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: mNuzm_OcEHf2BkvCQd8M_i2cVhiXti4C X-Proofpoint-GUID: mNuzm_OcEHf2BkvCQd8M_i2cVhiXti4C X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.957,Hydra:6.0.601,FMLib:17.11.176.26 definitions=2023-08-25_16,2023-08-25_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.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: INBOX X-GMAIL-THRID: 1775139152059308106 X-GMAIL-MSGID: 1775227058442777875 In PR 106677, I noticed that on the trunk we were producing: ``` _25 = SR.116_117 == 0; _27 = (unsigned char) _25; _32 = _27 | SR.116_117; ``` From `SR.115_117 != 0 ? SR.115_117 : 1` Rather than: ``` _119 = MAX_EXPR <1, SR.115_117>; ``` Or (rather) ``` _119 = SR.115_117 | 1; ``` Due to the order of the patterns. Committed as approved with the new comment and testcase. Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: * match.pd (`a ? one_zero : one_zero`): Move below detection of minmax. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/phi-opt-34.c: New test. --- gcc/match.pd | 42 ++++++++++++---------- gcc/testsuite/gcc.dg/tree-ssa/phi-opt-34.c | 23 ++++++++++++ 2 files changed, 47 insertions(+), 18 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-34.c diff --git a/gcc/match.pd b/gcc/match.pd index d9f35e9e25b..fa598d5ca2e 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4961,24 +4961,6 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) ) ) -(simplify - (cond @0 zero_one_valued_p@1 zero_one_valued_p@2) - (switch - /* bool0 ? bool1 : 0 -> bool0 & bool1 */ - (if (integer_zerop (@2)) - (bit_and (convert @0) @1)) - /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */ - (if (integer_zerop (@1)) - (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2)) - /* bool0 ? 1 : bool2 -> bool0 | bool2 */ - (if (integer_onep (@1)) - (bit_ior (convert @0) @2)) - /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */ - (if (integer_onep (@2)) - (bit_ior (bit_xor (convert @0) @2) @1)) - ) -) - /* Optimize # x_5 in range [cst1, cst2] where cst2 = cst1 + 1 x_5 ? cstN ? cst4 : cst3 @@ -5309,6 +5291,30 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @1))) (max @2 @4)))))) +#if GIMPLE +/* These patterns should be after min/max detection as simplifications + of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)` + and `(type)(zero_one^1)` are not done yet. See PR 110637. + Even without those, reaching min/max/and/ior faster is better. */ +(simplify + (cond @0 zero_one_valued_p@1 zero_one_valued_p@2) + (switch + /* bool0 ? bool1 : 0 -> bool0 & bool1 */ + (if (integer_zerop (@2)) + (bit_and (convert @0) @1)) + /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */ + (if (integer_zerop (@1)) + (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2)) + /* bool0 ? 1 : bool2 -> bool0 | bool2 */ + (if (integer_onep (@1)) + (bit_ior (convert @0) @2)) + /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */ + (if (integer_onep (@2)) + (bit_ior (bit_xor (convert @0) @2) @1)) + ) +) +#endif + /* 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-34.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-34.c new file mode 100644 index 00000000000..157c3ea9a0b --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-34.c @@ -0,0 +1,23 @@ +/* { dg-do compile } */ +/* Disable early phiopt1 as early ccp1 does not export non-zero bits + so at the point of phiopt1, we don't know that a is [0,1] range */ +/* { dg-options "-O1 -fdisable-tree-phiopt1 -fdump-tree-phiopt2-folding"} */ + +unsigned f(unsigned a) +{ + a &= 1; + if (a > 0) + return a; + return 1; +} +/* PHIOPT2 should be able to change this into just return 1; + (which was `MAX` or `a | 1` but since a is known to be a + range of [0,1], it should be folded into 1) + And not fold it into `(a == 0) | a`. */ +/* { dg-final { scan-tree-dump-not " == " "phiopt2" } } */ +/* { dg-final { scan-tree-dump-not " if " "phiopt2" } } */ +/* { dg-final { scan-tree-dump-not "Folded into the sequence:" "phiopt2" } } */ +/* { dg-final { scan-tree-dump "return 1;" "phiopt2" } } */ +/* We want to make sure that phiopt2 is happening and not some other pass + before it does the transformation. */ +/* { dg-final { scan-tree-dump "Removing basic block" "phiopt2" } } */