From patchwork Tue Aug 8 00:54:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 132468 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1801303vqr; Mon, 7 Aug 2023 17:55:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEgk9m162hiw/ju9B2dN4cKR/JZeI8HpSezEHXF4ohfSOuojIfqPhxIXQ71hRc1KRoCcNCk X-Received: by 2002:ac2:5192:0:b0:4fe:3a57:7c90 with SMTP id u18-20020ac25192000000b004fe3a577c90mr6372276lfi.19.1691456123015; Mon, 07 Aug 2023 17:55:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691456122; cv=none; d=google.com; s=arc-20160816; b=bBY5SacZlABOXbhksrEBzfzecHeVNP8TyOQ2LheTdwfVTLa3JzrnoXKPCYV8ZNy4Qr trpKUR5LWVNsm6Jbp+Xt3DQGFWjwhk8axKBCgpd3p43alLHwBaQCaXpBGAw/IRDE8/wL GJmBvoZjKvRE/B+EXrf6e2ORfvaYjxfg1HgVyzpVo4JzZqBkwEM2WXZMCtT7vTiSoOL9 GTpeYIjVYzt8aXnkRAB50rahS2GOP8HKMsu27OsuvBiZuCyVyrY0Rz3p7rHtQ4zDhihb vMcIYAPbzLm1wZloRcAG4BsAxP+RQmbdAYVAvqeenI4gNTFQojRJA8ZMbzZzCMj3EPJV yTjA== 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=rt4WPLD4AvmSRXhTEQdW674fU4dnhVEglY4Pa83exi8=; fh=09okz+w4tVyhQmqNBttR063P0zKcIsLE1JRBioNPGC4=; b=pLPP9xnjGISVqHBXq5nxyL+WszVhprBXV+i1CetK5zzkPDxysqVRABUwcJis/vnZ+q PN3de6EkvgRVfHrbVc2N1wCEuCR9RQkBJVw9hiQFOTHJ1TfXlNCLW8zEbVUmahhHb7Wi 5CT/2VHVTC5hGR4S+M9KaKOcgnD+IjzfHea87rob8iMK1cm+FP1FAuNhx9tEv7MZJ/e6 3S5RaGviclHCXzEHZv5sOPFkyfiaGdRTZX2RdYU5MLYo43ROrYVDMfQTjy+0+JM2gJbP Dr2krITmySB03i6AdLS+E+PC/+ZyH9j5J6kEOCMN0ntztVpb/Mza7VmO3X0doK4sG8/9 LqnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=gNpF2MJ6; 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 c16-20020aa7df10000000b005222ab8744bsi6260715edy.332.2023.08.07.17.55.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 17:55:22 -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=gNpF2MJ6; 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 A8EBA385842B for ; Tue, 8 Aug 2023 00:55:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A8EBA385842B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691456121; bh=rt4WPLD4AvmSRXhTEQdW674fU4dnhVEglY4Pa83exi8=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=gNpF2MJ6Rbt6NVxRxxS4P2hHN6q+ASB2QF0xrO/boIDJcKg41S3FMb0j3vy3TYilX RsaxYD0MUO482sZ8RJCKCvHcjjFQclCdTjhZSSKpADQnJRvcQml5FBUdpszpw39Rmc StupUbkSnPXwH4Epv2Cw/lreRo/YGqTDlE1mv0ws= 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 20F243858C41 for ; Tue, 8 Aug 2023 00:54:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 20F243858C41 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 377G3bNE008678 for ; Mon, 7 Aug 2023 17:54:34 -0700 Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3s9nxkxpny-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Mon, 07 Aug 2023 17:54:34 -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; Mon, 7 Aug 2023 17:54:32 -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; Mon, 7 Aug 2023 17:54:32 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id ECFE93F703F; Mon, 7 Aug 2023 17:54:31 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] MATCH: [PR110937/PR100798] (a ? ~b : b) should be optimized to b ^ -(a) Date: Mon, 7 Aug 2023 17:54:24 -0700 Message-ID: <20230808005424.2563140-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-GUID: OunZ7BYr0eku-szw_yy2ZfzonPiZx8Fu X-Proofpoint-ORIG-GUID: OunZ7BYr0eku-szw_yy2ZfzonPiZx8Fu X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-08-07_27,2023-08-03_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: 1773620295776313725 X-GMAIL-MSGID: 1773620295776313725 This adds a simple match pattern for this case. I noticed it a couple of different places. One while I was looking at code generation of a parser and also while I was looking at locations where bitwise_inverted_equal_p should be used more. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/110937 PR tree-optimization/100798 gcc/ChangeLog: * match.pd (`a ? ~b : b`): Handle this case. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/bool-14.c: New test. * gcc.dg/tree-ssa/bool-15.c: New test. * gcc.dg/tree-ssa/phi-opt-33.c: New test. * gcc.dg/tree-ssa/20030709-2.c: Update testcase so `a ? -1 : 0` is not used to hit the match pattern. --- gcc/match.pd | 13 +++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c | 5 +++-- gcc/testsuite/gcc.dg/tree-ssa/bool-14.c | 15 +++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/bool-15.c | 18 ++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c | 13 +++++++++++++ 5 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-14.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-15.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c diff --git a/gcc/match.pd b/gcc/match.pd index 9b4819e5be7..f887c517c81 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -6460,6 +6460,19 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (cmp == NE_EXPR) { constant_boolean_node (true, type); }))))))) +#if GIMPLE +/* a?~t:t -> (-(a))^t */ +(simplify + (cond @0 @1 @2) + (if (bitwise_inverted_equal_p (@1, @2)) + (with { + auto prec = TYPE_PRECISION (type); + auto unsign = TYPE_UNSIGNED (type); + tree inttype = build_nonstandard_integer_type (prec, unsign); + } + (convert (bit_xor (negate (convert:inttype @0)) (convert:inttype @2)))))) +#endif + /* Simplify pointer equality compares using PTA. */ (for neeq (ne eq) (simplify diff --git a/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c b/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c index 5009cd69cfe..78938f919d4 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c @@ -29,15 +29,16 @@ union tree_node }; int make_decl_rtl (tree, int); void * -get_alias_set (t) +get_alias_set (t, t1) tree t; + void *t1; { long set; if (t->decl.rtl) return (t->decl.rtl->fld[1].rtmem ? 0 : (((t->decl.rtl ? t->decl.rtl: (make_decl_rtl (t, 0), t->decl.rtl)))->fld[1]).rtmem); - return (void*)-1; + return t1; } /* There should be precisely one load of ->decl.rtl. If there is diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-14.c b/gcc/testsuite/gcc.dg/tree-ssa/bool-14.c new file mode 100644 index 00000000000..0149380a63b --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-14.c @@ -0,0 +1,15 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized-raw" } */ +/* PR tree-optimization/110937 */ + +_Bool f2(_Bool a, _Bool b) +{ + if (a) + return !b; + return b; +} + +/* We should be able to remove the conditional and convert it to an xor. */ +/* { 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" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-15.c b/gcc/testsuite/gcc.dg/tree-ssa/bool-15.c new file mode 100644 index 00000000000..1f496663863 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-15.c @@ -0,0 +1,18 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized-raw" } */ +/* PR tree-optimization/110937 */ + +_Bool f2(int x, int y, int w, int z) +{ + _Bool a = x == y; + _Bool b = w == z; + if (a) + return !b; + return b; +} + +/* We should be able to remove the conditional and convert it to an xor. */ +/* { dg-final { scan-tree-dump-not "gimple_cond " "optimized" } } */ +/* { dg-final { scan-tree-dump-not "gimple_phi " "optimized" } } */ +/* { dg-final { scan-tree-dump-not "ne_expr, " "optimized" } } */ +/* { dg-final { scan-tree-dump-times "bit_xor_expr, " 1 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c new file mode 100644 index 00000000000..b79fe44187a --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized-raw" } */ +/* PR tree-optimization/100798 */ + +int f(int a, int t) +{ + return (a=='s' ? ~t : t); +} + +/* This should be convert into t^-(a=='s'). */ +/* { dg-final { scan-tree-dump-times "bit_xor_expr, " 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "negate_expr, " 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-not "bit_not_expr, " "optimized" } } */