From patchwork Fri Aug 25 16:21:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 136926 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a7d1:0:b0:3f2:4152:657d with SMTP id p17csp1911286vqm; Fri, 25 Aug 2023 09:22:43 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHWPg1TpitMZKrBowinxzn6VLIgo9FTsA4RQskiJUguQVppJemZGCKOraEAP0x1G/lCLCio X-Received: by 2002:a17:906:51d6:b0:9a1:d087:e0bf with SMTP id v22-20020a17090651d600b009a1d087e0bfmr7431654ejk.43.1692980562863; Fri, 25 Aug 2023 09:22:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692980562; cv=none; d=google.com; s=arc-20160816; b=KkbsxIj2aq3cup3/1SSx8Vf9WLRl4j0X/dPHdWD6kXRXK8NSKbkwiT4dWXOdkduwSz blvDgIts1eel09jAxhGUxiUn4tt3rUZczdGTHh40BbmG6MID+FAYMUWt9kXFMBQhqjJ/ L757HoFhl4J5qQKA0f4xryu2DRGRrbeS7qObO5K2Ld/hZ5gymEdmPR8Bh6ylif6x1VO2 FzfQqgng15tnjUaRh9nlCWwOIjqxagkYQ00a7UwT8seyzt0Gm/shhappznw8Zhjl7vo2 GbLLnjXGtJG9NvO+K4KzDgMgmf3oEs7KD7KSS7qFWvsURW0wv3xCMzBL1l5Ufl3d2fiu cQlQ== 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=0pi8+bXh4QykL0rv4jS1Rn3T/uTm6Js02e5y+iu0bs0=; fh=XbcmRug6/SzczusyoNU1I7Lu5oY0AN2u0mWDzKoxdSs=; b=O4VAmF6Be75fqMTB5qBXnhRv1OsEtDPKWlc9a2FrqvNpKhgpQL64yV3jSkupPS7dy9 TQE58exCLDdR2CGgx99bc/L2oWsf3IytTIVZnKjyAPywC11qfOO/IA7yPy50kxmmBDTr ovrXz0oU22dqEbBbZsJ/69bZkBVCYAzeYixcifk6SUlWMjxRSJU/0o1aXeWmiENg/AJJ N/mOt3wt/qkqK86zGCYfDXXY9sx8x6zg25zp8x0pLJBqt+i+susgBEueKR91XYQp6ZAY HFyYe5bsaT+grIJL/QrmhHcgGofalJlXcf/+D7WSVmV3AFFRwKHUYf6OAfDjL7s6+i9p xT5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="qaTcAde/"; 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"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id y22-20020a170906449600b00997e1a4b954si1144303ejo.25.2023.08.25.09.22.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Aug 2023 09:22:42 -0700 (PDT) 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=pass header.i=@gcc.gnu.org header.s=default header.b="qaTcAde/"; 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"; 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 74E4B3858436 for ; Fri, 25 Aug 2023 16:22:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 74E4B3858436 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692980551; bh=0pi8+bXh4QykL0rv4jS1Rn3T/uTm6Js02e5y+iu0bs0=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=qaTcAde/AE7pLB/DXagxCw7hv2iH0V35gwPCN/twSWPQb7PI7esXeR3+Eb1wfBa09 mMs38qWdzrha76cEg/tKlBACAoeB/ZT/M5P01U2hSj5tIthTMFEe+2R6WezMIjR9Q2 7Ho9YKdz5kErI14o0SYKwnS04pRMk9G8EcPNeBOc= 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 AD6CE3858431 for ; Fri, 25 Aug 2023 16:21:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AD6CE3858431 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 37P3ctgY001856 for ; Fri, 25 Aug 2023 09:21:44 -0700 Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3spmk2a1q8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Fri, 25 Aug 2023 09:21:44 -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 09:21:42 -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 09:21:42 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 561A63F7093; Fri, 25 Aug 2023 09:21:42 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] MATCH: Move `(X & ~Y) | (~X & Y)` over to use bitwise_inverted_equal_p Date: Fri, 25 Aug 2023 09:21:22 -0700 Message-ID: <20230825162122.3599370-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: t1h5LLKiTKMBLWua4fw-UbwqxMjJUl5G X-Proofpoint-GUID: t1h5LLKiTKMBLWua4fw-UbwqxMjJUl5G 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_14,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: 1775218786365948589 X-GMAIL-MSGID: 1775218786365948589 This moves the pattern `(X & ~Y) | (~X & Y)` to use bitwise_inverted_equal_p so we can simplify earlier the case where X and Y are defined by comparisons. We were able to optimize to (!X)^(!Y) in the end due to the pattern added in r14-3110-g7fb65f102851248bafa0815 and the older pattern r13-4620-g4d9db4bdd458 . But folding it earlier is better. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. Note pr87009.c now gets `return x ^ s; in one case where the test had been expecting `return s ^ x;` both are valid and would be expectly the same; just we now chose a slightly different order of simplification which causes the order of the operands to be different. gcc/ChangeLog: * match.pd (`(X & ~Y) | (~X & Y)`): Use bitwise_inverted_equal_p instead of specifically checking for ~X. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/cmpbit-3.c: New test. * gcc.dg/pr87009.c: Update test. --- gcc/match.pd | 13 +++++----- gcc/testsuite/gcc.dg/pr87009.c | 2 +- gcc/testsuite/gcc.dg/tree-ssa/cmpbit-3.c | 33 ++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cmpbit-3.c diff --git a/gcc/match.pd b/gcc/match.pd index 70884bd48eb..e41403664d0 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1228,12 +1228,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */ (for op (bit_ior bit_xor plus) (simplify - (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1)) - (bit_xor @0 @1)) - (simplify - (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1)) - (if (~wi::to_wide (@2) == wi::to_wide (@1)) - (bit_xor @0 @1)))) + (op (bit_and:c @0 @2) (bit_and:c @3 @1)) + (with { bool wascmp0, wascmp1; } + (if (bitwise_inverted_equal_p (@2, @1, wascmp0) + && bitwise_inverted_equal_p (@0, @3, wascmp1) + && ((!wascmp0 && !wascmp1) + || element_precision (type) == 1)) + (bit_xor @0 @1))))) /* PR53979: Transform ((a ^ b) | a) -> (a | b) */ (simplify diff --git a/gcc/testsuite/gcc.dg/pr87009.c b/gcc/testsuite/gcc.dg/pr87009.c index eb8a4ecd920..6f0341d17cc 100644 --- a/gcc/testsuite/gcc.dg/pr87009.c +++ b/gcc/testsuite/gcc.dg/pr87009.c @@ -1,6 +1,6 @@ /* { dg-do compile } */ /* { dg-options "-O -fdump-tree-original" } */ -/* { dg-final { scan-tree-dump-times "return s \\^ x;" 4 "original" } } */ +/* { dg-final { scan-tree-dump-times "return s \\^ x;|return x \\^ s;" 4 "original" } } */ int f1 (int x, int s) { diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-3.c b/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-3.c new file mode 100644 index 00000000000..936c0934a10 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-3.c @@ -0,0 +1,33 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized-raw -fdump-tree-dse1-raw -fdump-tree-forwprop1" } */ + +_Bool f(int a, int b) +{ + _Bool X = a==1, Y = b == 2; +return (X & !Y) | (!X & Y); +} + + +_Bool f1(int a, int b) +{ + _Bool X = a==1, Y = b == 2; + _Bool c = (X & !Y); + _Bool d = (!X & Y); + return c | d; +} + +/* Both of these should be optimized to (a==1) ^ (b==2) or (a != 1) ^ (b != 2) */ +/* { dg-final { scan-tree-dump-not "gimple_cond " "optimized" } } */ +/* { dg-final { scan-tree-dump-not "gimple_phi " "optimized" } } */ +/* { dg-final { scan-tree-dump-times "ne_expr|eq_expr, " 4 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "bit_xor_expr, " 2 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "gimple_assign " 6 "optimized" } } */ + +/* Both of these should be optimized early in the pipeline after forwprop1 */ +/* { dg-final { scan-tree-dump-times "ne_expr|eq_expr, " 4 "forwprop1" { xfail *-*-* } } } */ +/* { dg-final { scan-tree-dump-times "bit_xor_expr, " 2 "forwprop1" { xfail *-*-* } } } */ +/* { dg-final { scan-tree-dump-times "gimple_assign " 6 "forwprop1" { xfail *-*-* } } } */ +/* Note forwprop1 does not remove all unused statements sometimes so test dse1 also. */ +/* { dg-final { scan-tree-dump-times "ne_expr|eq_expr, " 4 "dse1" } } */ +/* { dg-final { scan-tree-dump-times "bit_xor_expr, " 2 "dse1" } } */ +/* { dg-final { scan-tree-dump-times "gimple_assign " 6 "dse1" } } */