From patchwork Sat Sep 23 19:49:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 143990 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp345175vqu; Sat, 23 Sep 2023 12:50:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHDztvOPGYkRueHBDdtaxJ/sBni721DaPAduv3gxAD6Rg6miVEAhdK2EHCc5MBkX0AptSHE X-Received: by 2002:a5d:4a0c:0:b0:31f:a717:f1cf with SMTP id m12-20020a5d4a0c000000b0031fa717f1cfmr2223039wrq.56.1695498608175; Sat, 23 Sep 2023 12:50:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695498608; cv=none; d=google.com; s=arc-20160816; b=JF7L3UZmZt9OlT615AKEoxgOShXppSW3WeaGzIXk1b+qDJCbtXrxG+BYPlDA7Lfj/3 iZOHQqtzTfoLMU1zv2L8swmfPilKdYNsRoTy//SVJhIlANin1/XEnCuUqQ4Tw/RlC2w2 UYKDo9r4HSPc19uGA7vEjshpdVMRmWZF4Z1fR66JCIyZLQKpLSBRF9Jw3fPW+Axj4+AR A+Dcfrt871V7P+OREci4iIFG7vG49Cm8r6/ICn9G/OXVlIWq6i/X8PI07sM1Ax79Pz5V EqJglIapRcsEZqCBK22CbxtzaV7Yo7tHjUgOZnSBRbRa6un/3q7i7cKr2/fYjZMScohZ Uw2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :dmarc-filter:delivered-to; bh=ahULeUZOCLtPhwO1tIKA5Lr2JlsiMagMOu+mtkxpb+w=; fh=XbcmRug6/SzczusyoNU1I7Lu5oY0AN2u0mWDzKoxdSs=; b=s1vOOeD5yIkszjZvUyasmR3tIpBS8gPZKPK/MewW07TYqEVFfDlldKtBYey7ZDW5vk b+LsFWp/95Az7sUshzcxd4HQRDLuZ8kNVTz2ARVMe4KdZ32RnnTnSaeUqp06jJ1aJaYW 2qNy/kgIqeI9AsGVMCV1Vh2IaQG4k6nikWx0h/g2QZFwizVXazHmLNUkail7qNGeU8b0 w5bOB13nHxu7uOVi1DjgIvwLNI1J2KlByLzWJdzdDpn8rHIOiRJf0LBGWERz3E6fdmfc W1G6nKleWQvcYhIntcDosViXlDsG1UInNgU61/DeS5vDMXYnyPNYArJk8MwtF4+yQwb1 3EXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@marvell.com header.s=pfpt0220 header.b=LT+QZE3S; 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=REJECT dis=NONE) header.from=marvell.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id jj1-20020a170907984100b009877bb42154si5701386ejc.465.2023.09.23.12.50.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 12:50:08 -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=@marvell.com header.s=pfpt0220 header.b=LT+QZE3S; 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=REJECT dis=NONE) header.from=marvell.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CA8943858C2C for ; Sat, 23 Sep 2023 19:50:02 +0000 (GMT) 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 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 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 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: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777859148544312340 X-GMAIL-MSGID: 1777859148544312340 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" } } */