From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0b-0016f401.pphosted.com (mx0a-0016f401.pphosted.com [67.231.148.174]) by sourceware.org (Postfix) with ESMTPS id 00C653858C66 for ; Sat, 23 Sep 2023 19:49:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 00C653858C66 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=marvell.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=marvell.com 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 38NJP0l6006299 for ; Sat, 23 Sep 2023 12:49:36 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marvell.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding : content-type; s=pfpt0220; bh=ahULeUZOCLtPhwO1tIKA5Lr2JlsiMagMOu+mtkxpb+w=; b=LT+QZE3Sz6biI4dCjYJPsgyBn47dIaLP2LmsPZbx4jSuJF1MPJ+CvNUdAf1PSH3P6s+5 zhR8fKL4Kvua2MQfVPQj1jn0nlIUclwhzCwXoNwxGLN2e9MwyC3wZITPv4LdRJH6KUw1 ka2zj//Ymtww4x39EjvGsmmAAfLQGkACNuePQj9IKzkMK5mhJ99X+lgANyIq1A3DmrEH d65rCTKxhCC3ePcrHJHMtDEnsjK/kgpJPJzrVKC5mMdS0gGMuh/Yj9RNvX8VwRfhU69z XFObF7BGvvN5JMNjyumiMx97JCKTEQ/qLiEPNJHJ61aGKGlE2TqhAsskAB6fml3XdZ2g pQ== Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3t9wcqh1u7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Sat, 23 Sep 2023 12:49:36 -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; Sat, 23 Sep 2023 12:49:35 -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; Sat, 23 Sep 2023 12:49:35 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id E000B3F7065; Sat, 23 Sep 2023 12:49:34 -0700 (PDT) From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH] MATCH: Add `(X & ~Y) & Y` and `(X | ~Y) | Y` Date: Sat, 23 Sep 2023 12:49:21 -0700 Message-ID: <20230923194921.2101021-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Proofpoint-ORIG-GUID: x7ObgVeEvXADX--VPFvLeUSK08mYdfwy X-Proofpoint-GUID: x7ObgVeEvXADX--VPFvLeUSK08mYdfwy X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-09-23_17,2023-09-21_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 List-Id: Even though this gets optimized by reassociation, catching it more often will always be better. Note the reason why I didn't add `(X ^ ~Y) ^ Y` is that it gets caught by prefering `~(X ^ Y)` to `(X ^ ~Y)` which then it is caught by the the pattern for `(X ^ Y) ^ Y` already. PR tree-optimization/111543 gcc/ChangeLog: * match.pd (`(X & ~Y) & Y`, `(X | ~Y) | Y`): New patterns. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/bitops-4.c: New test. --- gcc/match.pd | 17 +++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/bitops-4.c | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bitops-4.c diff --git a/gcc/match.pd b/gcc/match.pd index 0aa815f4118..a17778fbaa6 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1806,6 +1806,23 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (simplify (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1)) (convert @0)) + +/* (X & ~Y) & Y -> 0 */ +(simplify + (bit_and:c (bit_and @0 @1) @2) + (with { bool wascmp; } + (if (bitwise_inverted_equal_p (@0, @2, wascmp) + || bitwise_inverted_equal_p (@1, @2, wascmp)) + { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); }))) +/* (X | ~Y) | Y -> -1 */ +(simplify + (bit_ior:c (bit_ior @0 @1) @2) + (with { bool wascmp; } + (if ((bitwise_inverted_equal_p (@0, @2, wascmp) + || bitwise_inverted_equal_p (@1, @2, wascmp)) + && (!wascmp || element_precision (type) == 1)) + { build_all_ones_cst (TREE_TYPE (@0)); }))) + /* (X & Y) & (X & Z) -> (X & Y) & Z (X | Y) | (X | Z) -> (X | Y) | Z */ (for op (bit_and bit_ior) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bitops-4.c b/gcc/testsuite/gcc.dg/tree-ssa/bitops-4.c new file mode 100644 index 00000000000..73c8f39d28f --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/bitops-4.c @@ -0,0 +1,18 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized -fdump-tree-ccp1" } */ +/* PR tree-optimization/111543 */ + +void f_or(int a, int b, int *por) +{ + int c = ~a; + *por = (c | b) | a; +} +void f_and(int a, int b, int *pand) +{ + int c = ~a; + *pand = (c & b) & a; +} +/* { dg-final { scan-tree-dump-times "pand_\[0-9\]+.D. = 0" 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "por_\[0-9\]+.D. = -1" 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "pand_\[0-9\]+.D. = 0" 1 "ccp1" } } */ +/* { dg-final { scan-tree-dump-times "por_\[0-9\]+.D. = -1" 1 "ccp1" } } */ -- 2.31.1