From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x629.google.com (mail-ej1-x629.google.com [IPv6:2a00:1450:4864:20::629]) by sourceware.org (Postfix) with ESMTPS id 427233858297 for ; Wed, 3 Aug 2022 20:45:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 427233858297 Received: by mail-ej1-x629.google.com with SMTP id gk3so21312366ejb.8 for ; Wed, 03 Aug 2022 13:45:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=2f8Jz30x9P/edW6Uh3d3gJ9iIeycuGuBPBKe/RyNs8A=; b=nMrgsf8VMyhcu6ZJWWu26DAita0UgbpmKWC6dhsXp9vk1xKA9S5EuW9bU+2JvI7JdY kHPmAuZM+8eguFDSd3AktGTo//+mpWUl8gRzjIqZakulMVRywiFeubmWkvMitYyI+exq zEgKwj7v2z9xuDwz98CmSflmXi/XITdnQ7a1xnRlFMNy3i/NoNFsFvgD8Crv7ggvzWyO SCgKFrXrTscVDIBvrpktetS1mUxDz8029jPFHRM8jtRlYTGBadDQEXin4ySnUcO0q4qI JYRqGy3kZQPYvAP1mlKOwqW7x/wB5jkRZI1h9vMNZA2w6SEpEY0SgFbI3hbFqZYWVIJH tAtg== X-Gm-Message-State: AJIora/UyXn72ng+Px1lCR8w9ZEAum7UnCwjpwgytzhJivTB3zSsMlEW YCMRPad7ro91kgc8vFJ7pto4M0vhXRnY5XttHH/mgbENO5RLKp5z X-Google-Smtp-Source: AGRyM1sJL4Iff1fCFzcbCGPE/CCfWo6lmFAh8HPYqQzURTqJYbdj/+XrMVu4SZ3XO5EU0Kyd+6baYd+YWH8YaN2Nsmw= X-Received: by 2002:a17:907:d8b:b0:72f:4645:1730 with SMTP id go11-20020a1709070d8b00b0072f46451730mr22156639ejc.724.1659559518415; Wed, 03 Aug 2022 13:45:18 -0700 (PDT) MIME-Version: 1.0 References: <20220803191014.2284344-1-sfeifer@redhat.com> In-Reply-To: <20220803191014.2284344-1-sfeifer@redhat.com> From: Prathamesh Kulkarni Date: Thu, 4 Aug 2022 02:14:41 +0530 Message-ID: Subject: Re: [PATCH] match.pd: Add bitwise and pattern [PR106243] To: Sam Feifer Cc: GCC Patches Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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-List-Received-Date: Wed, 03 Aug 2022 20:45:32 -0000 On Thu, 4 Aug 2022 at 00:41, Sam Feifer via Gcc-patches wrote: > > This patch adds a new optimization to match.pd. The pattern, -x & 1, > now gets simplified to x & 1, reducing the number of instructions > produced. Hi Sam, No comments on patch, but wondering if we can similarly add another pattern to simplify abs(x) & 1 -> x & 1 ? Currently we don't appear to do it on GIMPLE: __attribute__((noipa)) int f1 (int x) { return __builtin_abs (x) & 1; } .optimized dump shows: _1 = ABS_EXPR ; _3 = _1 & 1; return _3; altho combine simplifies it to x & 1 on RTL, resulting in code-gen: f1: and w0, w0, 1 ret Thanks, Prathamesh > > This patch also adds tests for the optimization rule. > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > PR tree-optimization/106243 > > gcc/ChangeLog: > > * match.pd (-x & 1): New simplification. > > gcc/testsuite/ChangeLog: > > * gcc.dg/pr106243-1.c: New test. > * gcc.dg/pr106243.c: New test. > --- > gcc/match.pd | 5 ++++ > gcc/testsuite/gcc.dg/pr106243-1.c | 18 +++++++++++++ > gcc/testsuite/gcc.dg/pr106243.c | 43 +++++++++++++++++++++++++++++++ > 3 files changed, 66 insertions(+) > create mode 100644 gcc/testsuite/gcc.dg/pr106243-1.c > create mode 100644 gcc/testsuite/gcc.dg/pr106243.c > > diff --git a/gcc/match.pd b/gcc/match.pd > index 562138a8034..78b32567836 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -8061,3 +8061,8 @@ and, > (if (TYPE_UNSIGNED (TREE_TYPE (@0))) > (bit_and @0 @1) > (cond (le @0 @1) @0 (bit_and @0 @1)))))) > + > +/* -x & 1 -> x & 1. */ > +(simplify > + (bit_and:c (negate @0) integer_onep@1) > + (bit_and @0 @1)) > diff --git a/gcc/testsuite/gcc.dg/pr106243-1.c b/gcc/testsuite/gcc.dg/pr106243-1.c > new file mode 100644 > index 00000000000..b1dbe5cbe44 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr106243-1.c > @@ -0,0 +1,18 @@ > +/* PR tree-optimization/106243 */ > +/* { dg-do run } */ > +/* { dg-options "-O2" } */ > + > +#include "pr106243.c" > + > +int main () { > + > + if (foo(3) != 1 > + || bar(-6) != 0 > + || baz(17) != 1 > + || qux(-128) != 0 > + || foo(127) != 1) { > + __builtin_abort(); > + } > + > + return 0; > +} > diff --git a/gcc/testsuite/gcc.dg/pr106243.c b/gcc/testsuite/gcc.dg/pr106243.c > new file mode 100644 > index 00000000000..ee2706f2bf9 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr106243.c > @@ -0,0 +1,43 @@ > +/* PR tree-optimization/106243 */ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-optimized" } */ > + > +#define vector __attribute__((vector_size(4*sizeof(int)))) > + > +/* Test from PR. */ > +__attribute__((noipa)) int foo (int x) { > + return -x & 1; > +} > + > +/* Other test from PR. */ > +__attribute__((noipa)) int bar (int x) { > + return (0 - x) & 1; > +} > + > +/* Forward propogation. */ > +__attribute__((noipa)) int baz (int x) { > + x = -x; > + return x & 1; > +} > + > +/* Commutative property. */ > +__attribute__((noipa)) int qux (int x) { > + return 1 & -x; > +} > + > +/* Vector test case. */ > +__attribute__((noipa)) vector int waldo (vector int x) { > + return -x & 1; > +} > + > +/* Should not simplify. */ > +__attribute__((noipa)) int thud (int x) { > + return -x & 2; > +} > + > +/* Should not simplify. */ > +__attribute__((noipa)) int corge (int x) { > + return -x & -1; > +} > + > +/* { dg-final {scan-tree-dump-times "-" 2 "optimized" } } */ > > base-commit: 388fbbd895e72669909173c3003ae65c6483a3c2 > -- > 2.31.1 >