From patchwork Sun Sep 3 16:25:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 137430 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ab0a:0:b0:3f2:4152:657d with SMTP id m10csp663743vqo; Sun, 3 Sep 2023 09:26:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFlI8l0O1vCA7vRVtVemozfIPJIoQ1NS11l+ZDeSP3mzJO7xb8bAK8rB4GUKWV92UFYFotA X-Received: by 2002:a05:6512:3701:b0:500:7e64:cff1 with SMTP id z1-20020a056512370100b005007e64cff1mr4482719lfr.14.1693758414822; Sun, 03 Sep 2023 09:26:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693758414; cv=none; d=google.com; s=arc-20160816; b=DBczj8OeJrrfWwJLBAYzMruIJ2vEFmBCOV2VokoFshv4Jx/0ZKWxY4KZivpOJ89lGY 1kZ4LkxxWP7+ctPXq9uDUA/CrXMmTstjEOrqvMaOYKI9/l32sfqQDL8UutJ/99Ugcayq sgR3NXAXRexK55cEOPBSo/u831JQ+UKol8EvrUtM02HeOu4Blguxj8yu5M+ckPxNHyac wk+Lwfk/CiCJVdniZDpsK7QJDfUHGOk01LmGQ9voElZvj85DbQfJStuIP4EFrJ0rnXW1 T0PJIPwD/rHBkXs82fNvH17u8VuM9YaCySrHNDf+0fEhnGpcrmYB4BP1aBMHpApXeABv t/sw== 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=2N3HNMnYicUfz4/ldUniuhMBeCkbyTo3bo7IjXC8oRg=; fh=XbcmRug6/SzczusyoNU1I7Lu5oY0AN2u0mWDzKoxdSs=; b=dbdK8qL4OoE8gHSUvKSDNNe+UEWyF6HBfbRsMAjgYD/yTAv0YDAnIY2gNYysmAMCKw iDNT+02J+rWxDOYV35XRoZM1vsBzKx5KiZReLglkXSm3znA9BEAe5mBPAeOk4gLCRn3o VLdTTCgdVnRug2NUNJ6RxGa5GxR16MZIh587vNoRZ/d7w692sLurky3AcHkHMq5Qbf4n qL3hP6C7XSpVt/NBX9ZfWgGPDGZGPnpnsS36b4l8nW1Sgn0LyJ75x7QxM6QPkulwSM/5 27FpNMrrv8267U+/ep0jzZEbYQMy/wf8JM+PlBwjlPbaVAwc7xexvu5Azj56Z6SOLXP4 sXfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=ami2JAsb; 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 v18-20020a056402185200b00523d83c4c11si5321435edy.54.2023.09.03.09.26.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Sep 2023 09:26:54 -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=ami2JAsb; 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 7BEDF3858C52 for ; Sun, 3 Sep 2023 16:26:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7BEDF3858C52 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1693758413; bh=2N3HNMnYicUfz4/ldUniuhMBeCkbyTo3bo7IjXC8oRg=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=ami2JAsb0KfHG3ntuHcO1QMkbkrQpYfV6nKak/s/He3C1mbeSl8azvwhCkTjum2e0 1oJ2Sr8Cu82noJmmcwLi4JM51u64UN4GsUTPeJCFvJFuh7q/UmsHXP8oxz4l0pvhkf uUj0CFbX8dZY5TIdQuI/McFyi6f2tY69H/JX7PMg= 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 D068C3858D33 for ; Sun, 3 Sep 2023 16:26:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D068C3858D33 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 3836bAXI003849 for ; Sun, 3 Sep 2023 09:26:08 -0700 Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3sv2dp3335-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Sun, 03 Sep 2023 09:26:07 -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; Sun, 3 Sep 2023 09:26:06 -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; Sun, 3 Sep 2023 09:26:06 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id D8E9E3F7070; Sun, 3 Sep 2023 09:26:05 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] MATCH: Transform `(1 >> X) !=/== 0` into `X ==/!= 0` Date: Sun, 3 Sep 2023 09:25:54 -0700 Message-ID: <20230903162554.908044-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: 05hDWtMVfHFngh7JioGtX6J_-gKvIWnL X-Proofpoint-GUID: 05hDWtMVfHFngh7JioGtX6J_-gKvIWnL 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-09-03_14,2023-08-31_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: , 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: 1776034423181804037 X-GMAIL-MSGID: 1776034423181804037 We currently have a pattern for handling `(C >> X) & D == 0` but if C is 1 and D is 1, the `& 1` might have been removed. gcc/ChangeLog: PR tree-optimization/105832 * match.pd (`(1 >> X) != 0`): New pattern gcc/testsuite/ChangeLog: PR tree-optimization/105832 * gcc.dg/tree-ssa/pr105832-1.c: New test. * gcc.dg/tree-ssa/pr105832-2.c: New test. * gcc.dg/tree-ssa/pr105832-3.c: New test. --- gcc/match.pd | 10 ++++- gcc/testsuite/gcc.dg/tree-ssa/pr105832-1.c | 25 ++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr105832-2.c | 30 ++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr105832-3.c | 46 ++++++++++++++++++++++ 4 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr105832-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr105832-2.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr105832-3.c diff --git a/gcc/match.pd b/gcc/match.pd index 5270e4104ac..e9ce48ea7fa 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4026,7 +4026,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* Simplify ((C << x) & D) != 0 where C and D are power of two constants, either to false if D is smaller (unsigned comparison) than C, or to - x == log2 (D) - log2 (C). Similarly for right shifts. */ + x == log2 (D) - log2 (C). Similarly for right shifts. + Note for `(1 >> x)`, the & 1 has been removed so matching that seperately. */ (for cmp (ne eq) icmp (eq ne) (simplify @@ -4043,7 +4044,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) int c2 = wi::clz (wi::to_wide (@2)); } (if (c1 > c2) { constant_boolean_node (cmp == NE_EXPR ? false : true, type); } - (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))) + (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); }))))) + /* `(1 >> X) != 0` -> `X == 0` */ + /* `(1 >> X) == 0` -> `X != 0` */ + (simplify + (cmp (rshift integer_onep @0) integer_zerop) + (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))) /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1) (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr105832-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr105832-1.c new file mode 100644 index 00000000000..d7029d39c85 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr105832-1.c @@ -0,0 +1,25 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -fdump-tree-optimized" } */ +/* PR tree-optimization/105832 */ + +void foo(void); + +static struct { + short a; + signed char b; +} c; + +static signed char d; + +int main() { + signed char g = c.b > 4U ? c.b : c.b << 2; + for (int h = 0; h < 5; h++) { + d = (g >= 2 || 1 >> g) ? g : g << 1; + if (d && 1 == g) + foo(); + c.a = 0; + } +} + +/* The call of foo should have been removed. */ +/* { dg-final { scan-tree-dump-not "foo " "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr105832-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr105832-2.c new file mode 100644 index 00000000000..2d2a33e2755 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr105832-2.c @@ -0,0 +1,30 @@ +/* PR tree-optimization/105832 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-original" } */ +/* { dg-final { scan-tree-dump "return a == 0;" "original" } } */ +/* { dg-final { scan-tree-dump "return b == 0;" "original" } } */ +/* { dg-final { scan-tree-dump "return c != 0;" "original" } } */ +/* { dg-final { scan-tree-dump "return d != 0;" "original" } } */ + +int +f1 (int a) +{ + return (1 >> a) != 0; +} + +int +f2 (int b) +{ + return ((1 >> b) & 1) != 0; +} +int +f3 (int c) +{ + return (1 >> c) == 0; +} + +int +f4 (int d) +{ + return ((1 >> d) & 1) == 0; +} diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr105832-3.c b/gcc/testsuite/gcc.dg/tree-ssa/pr105832-3.c new file mode 100644 index 00000000000..2bdd9afcbc7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr105832-3.c @@ -0,0 +1,46 @@ +/* PR tree-optimization/105832 */ +/* { dg-do compile } */ +/* Disable the first forwprop1 as that will catch f2/f4 even though `&1` + will be removed during evrp. */ +/* { dg-options "-O2 -fdisable-tree-forwprop1 -fdump-tree-optimized" } */ +/* { dg-final { scan-tree-dump "a_\[0-9]+\\(D\\) == 0" "optimized" } } */ +/* { dg-final { scan-tree-dump "b_\[0-9]+\\(D\\) == 0" "optimized" } } */ +/* { dg-final { scan-tree-dump "c_\[0-9]+\\(D\\) != 0" "optimized" } } */ +/* { dg-final { scan-tree-dump "d_\[0-9]+\\(D\\) != 0" "optimized" } } */ + +int g(void); +int h(void); + +int +f1 (int a) +{ + int t = 1 >> a; + if (t != 0) return g(); + return h(); +} + +int +f2 (int b) +{ + int t = 1 >> b; + t &= 1; + if (t != 0) return g(); + return h(); +} + +int +f3 (int c) +{ + int t = 1 >> c; + if (t == 0) return g(); + return h(); +} + +int +f4 (int d) +{ + int t = 1 >> d; + t &= 1; + if (t == 0) return g(); + return h(); +}