From patchwork Thu Aug 24 02:38:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 136743 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a7d1:0:b0:3f2:4152:657d with SMTP id p17csp847123vqm; Wed, 23 Aug 2023 19:39:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHhBWR09ToSiSVdCDpcNPBQgawQKrCjN0yv2xVTNHa+zpx7QPXSIa/rdqOeXgrSvB0+la8R X-Received: by 2002:aa7:d502:0:b0:51e:309:2e12 with SMTP id y2-20020aa7d502000000b0051e03092e12mr12297420edq.28.1692844757342; Wed, 23 Aug 2023 19:39:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692844757; cv=none; d=google.com; s=arc-20160816; b=MkpbAjHhN6X8e1xdx7eslXqiFk/lQ+V+MOgyBmiHFIzOw9eoNv5sEa/GgwAvdNxTbD R88E1/XpdqpnX3WlXaAPnGLYbMx23HVLMi48RSolvZThPmz+OEevuyQV4e44+exCrEll HFkxGwJtKfNWVsHwY+K+V4b+kov1UR2u802FT1Xk3V04PWYyuANr/GKOeN+w+C8WbgxP ixG+Uw23RCvnPCtJlCeV4fm3qNZpmampxMOiECKSyzQrlYUmFe+tz/T9lbD27KRSzk+F BHvPb04InWvqPdJ+LazuowOS3T+yqtC2VK8wid7ig0qaZ4vQHU4iF+mJHIdKQYQ1KrX9 1NRg== 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=LA9jIISAT+6KTtHFuD/A2jnAwpgi4o3K6qoLzmS4b0w=; fh=09okz+w4tVyhQmqNBttR063P0zKcIsLE1JRBioNPGC4=; b=urs2wRKpRlgYdPOXwqTkJt4jhcFR5Ikrm/LWFXn90Rlsb54F9kAD+q814MV4vthqcm 6Ql5k4Q23KdG3eZUJStH65UaDYEdd32bBsNY+Q5E6/qkoJeaRXYdT+jZGccVCqg4ZDcH f99pvHPiBbdEcL1g8rVFMh0nOoGIm3swjjS/Plbynop5tvEEwhB/DEFRpkpNAqmglfF2 NQajkIvgppw7lE9q7sKZusRDoyS17u1CjPKzVtD44c+njxz7WtzqFQeVbQGdRIotvP0u meQAFWg03JswE1Xpj4p99//JOH2pk64itasGmuhfiPmbmmoxNpa5FYoHB1RVLiSS0zds d0OQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=CIHf43ub; 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 t20-20020a056402021400b00525b997a7b9si9684865edv.610.2023.08.23.19.39.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Aug 2023 19:39:17 -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=CIHf43ub; 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 35AB0385C421 for ; Thu, 24 Aug 2023 02:39:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 35AB0385C421 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692844754; bh=LA9jIISAT+6KTtHFuD/A2jnAwpgi4o3K6qoLzmS4b0w=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=CIHf43ub659E+08TKj6faMStO/uK4nrp4DCPGUq9O74CnjPXGJ+aGHctrtTaFFocS zWYFgunA5vu/06YSPhVpWAMYuLlDfwAfcNqYrZKMDNRvC8yFev9XZ6KbZTF2CEJn/U IVb98KVviHXufsJPACgvVCONKdBizcnAyYQQ8y8c= 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 B1C3E3858C01 for ; Thu, 24 Aug 2023 02:38:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B1C3E3858C01 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 37NHwg97001929 for ; Wed, 23 Aug 2023 19:38:28 -0700 Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3sn20cwta0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Wed, 23 Aug 2023 19:38:27 -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; Wed, 23 Aug 2023 19:38:25 -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; Wed, 23 Aug 2023 19:38:25 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 115533F705D; Wed, 23 Aug 2023 19:38:24 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] MATCH: remove negate for 1bit types Date: Wed, 23 Aug 2023 19:38:15 -0700 Message-ID: <20230824023815.3506414-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-GUID: csPJa-yHz7G316bTP4-XTo3YV4zVXWsz X-Proofpoint-ORIG-GUID: csPJa-yHz7G316bTP4-XTo3YV4zVXWsz 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-23_17,2023-08-22_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: 1775076383958970739 X-GMAIL-MSGID: 1775076383958970739 For 1bit types, negate is either undefined or don't change the value. In either cases we want to remove them. This patch adds a match pattern to do that. Also converting to a 1bit type we can remove the negate just like we already do for `&1` so this patch adds that too. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. Notes on the testcases: This patch is the last part to fix PR 95929; cond-bool-2.c testcase. bit1neg-1.c is a 1bit-field testcase where we could remove the assignment all the way in one case (which happened on the RTL level for some targets but not all). cond-bool-2.c is the reduced testcase of PR 95929. PR tree-optimization/95929 gcc/ChangeLog: * match.pd (convert?(-a)): New pattern for 1bit integer types. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/bit1neg-1.c: New test. * gcc.dg/tree-ssa/cond-bool-1.c: New test. * gcc.dg/tree-ssa/cond-bool-2.c: New test. --- gcc/match.pd | 12 ++++++++++ gcc/testsuite/gcc.dg/tree-ssa/bit1neg-1.c | 23 ++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/cond-bool-1.c | 21 +++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/cond-bool-2.c | 26 +++++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bit1neg-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cond-bool-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cond-bool-2.c diff --git a/gcc/match.pd b/gcc/match.pd index a2e56d5a4e8..3bbeceb37b4 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -9090,6 +9090,18 @@ and, (if (!TYPE_OVERFLOW_SANITIZED (type)) (bit_and @0 @1))) +/* `-a` is just `a` if the type is 1bit wide or when converting + to a 1bit type; similar to the above transformation of `(-x)&1`. + This is used mostly with the transformation of + `a ? ~b : b` into `(-a)^b`. + It also can show up with bitfields. */ +(simplify + (convert? (negate @0)) + (if (INTEGRAL_TYPE_P (type) + && TYPE_PRECISION (type) == 1 + && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))) + (convert @0))) + /* Optimize c1 = VEC_PERM_EXPR (a, a, mask) c2 = VEC_PERM_EXPR (b, b, mask) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bit1neg-1.c b/gcc/testsuite/gcc.dg/tree-ssa/bit1neg-1.c new file mode 100644 index 00000000000..2f123fbb9b5 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/bit1neg-1.c @@ -0,0 +1,23 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +struct f +{ + int a:1; +}; + +void g(struct f *a) +{ + int t = a->a; + t = -t; + a->a = t; +} +void g1(struct f *a, int b) +{ + int t = b; + t = -t; + a->a = t; +} +/* the 2 negates should have been removed as this is basically the same + as (-a) & 1. */ +/* { dg-final { scan-tree-dump-not " = -" "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cond-bool-1.c b/gcc/testsuite/gcc.dg/tree-ssa/cond-bool-1.c new file mode 100644 index 00000000000..752a3030ad1 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/cond-bool-1.c @@ -0,0 +1,21 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized-raw" } */ +_Bool f1(int a, int b) +{ + _Bool _1 = b != 0; + _Bool _2 = a != 0; + _Bool _8 = a == 0; + _Bool _13; + if (_1) _13 = _8; else _13 = _2; + return _13; +} + +/* We should be able to optimize this to (a != 0) ^ (b != 0) */ +/* There should be no negate_expr nor gimple_cond here. */ + +/* { dg-final { scan-tree-dump-not "negate_expr, " "optimized" } } */ +/* { dg-final { scan-tree-dump-times "ne_expr, " 2 "optimized" } } */ +/* { dg-final { scan-tree-dump-not "gimple_cond " "optimized" } } */ +/* { dg-final { scan-tree-dump-not "gimple_phi " "optimized" } } */ +/* { dg-final { scan-tree-dump-times "bit_xor_expr, " 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "gimple_assign " 3 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cond-bool-2.c b/gcc/testsuite/gcc.dg/tree-ssa/cond-bool-2.c new file mode 100644 index 00000000000..b3e7e25dec6 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/cond-bool-2.c @@ -0,0 +1,26 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized-raw" } */ + +/* PR tree-optimization/95929 */ + + +static inline _Bool nand(_Bool a, _Bool b) +{ + return !(a && b); +} + +_Bool f(int a, int b) +{ + return nand(nand(b, nand(a, a)), nand(a, nand(b, b))); +} + +/* We should be able to optimize this to (a != 0) ^ (b != 0) */ +/* There should be no negate_expr nor gimple_cond here. */ + +/* { dg-final { scan-tree-dump-not "negate_expr, " "optimized" } } */ +/* { dg-final { scan-tree-dump-times "ne_expr, " 2 "optimized" } } */ +/* { dg-final { scan-tree-dump-not "gimple_cond " "optimized" } } */ +/* { dg-final { scan-tree-dump-not "cond_expr, " "optimized" } } */ +/* { dg-final { scan-tree-dump-not "gimple_phi " "optimized" } } */ +/* { dg-final { scan-tree-dump-times "bit_xor_expr, " 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "gimple_assign " 3 "optimized" } } */