From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by sourceware.org (Postfix) with ESMTPS id 6C3903858CDB for ; Thu, 20 Jul 2023 15:17:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6C3903858CDB Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-668709767b1so608007b3a.2 for ; Thu, 20 Jul 2023 08:17:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689866233; x=1690471033; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Azz+v2lWmQiG4mcV9R3EXrej6sHKcMZ2QcIMAvarGwc=; b=OWYrORIfNaSx+7jNPRfO8WcziWBgBskKjx0garJrsAHlboPxOZKmEb/WXxLEqtZzYY zYDKXlGMQOtVldwrKCTph98p1uvbXlj9GLLUHktpUpOV5TaKrQE6+1pRkQqy39FhapMx 1L8Yie0Xmer4V27qHXtcfF5pqrGQbh7k8Tz/kz7/AOys4LVcETmFn4HTUfPPewvApOel 2Z9yfuH+qFMK38a1FMzB/Cs9gbllSnSdNE0M2GBMGv/HmXnp3H0VevXf6oYLX6cj6YSQ LkNoP+2zL7iuWq+dzD01FQqrfjplsa6fUi7ZuLbSQbukwenxFbFCCgIAu5lR/jbNo7M8 a7mQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689866233; x=1690471033; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Azz+v2lWmQiG4mcV9R3EXrej6sHKcMZ2QcIMAvarGwc=; b=YPItCe03uNXLXhr232cx8anVQ+vxdkNwyINcQr8DWe1ZFozt4OYf6vgmg6pnWqo9T3 z0xr9cCDg+EEKaxNH0h1FQKFtaxqT1Vri6maBaI9WB+mRz0FvCwULw4ualdrvZosm5iO Q/x4JlrTnSxppm6Ah+V1GmXj1RFuEZwsb+Web1Eu/I/8gRMuLRWRixAoxIAsK+KB7rOf 0SLnSwcdg0wK7AcfV9gsQhamtF3OntzEoKs0ujYsc+BAQ44UL/8gHGsGHaBq/U4VFw5l A5ModNWk6uBJAnW5ioXiALi+NjSv3d49SGwMRsQlrzuyQni306eu8lM1JzX7fB/OPXaq LAuA== X-Gm-Message-State: ABy/qLa0vJacDKFSsV2+0e3TDilOGOD/OtjcWjKqirxB/7dVa8FvOXI/ vhlqp44zdTaQWVHtTXOLQ/Z+8seCL5CxXDRN6x3yF4DWrSU= X-Google-Smtp-Source: APBJJlGbkO8/oh/vD7nysTIGgDs/71TKqGgAzmBbSqDCp5zEct+xJljGOQ4UixgbvsWhhxayVVF5bRL/PJ0RUTtSe9Y= X-Received: by 2002:a05:6a20:3d22:b0:133:5352:c7ac with SMTP id y34-20020a056a203d2200b001335352c7acmr238080pzi.38.1689866232896; Thu, 20 Jul 2023 08:17:12 -0700 (PDT) MIME-Version: 1.0 References: <20230720144711.60975-1-drross@redhat.com> In-Reply-To: <20230720144711.60975-1-drross@redhat.com> From: Andrew Pinski Date: Thu, 20 Jul 2023 08:17:01 -0700 Message-ID: Subject: Re: [PATCH] match.pd: Implement missed optimization (x << c) >> c -> -(x & 1) [PR101955] To: Drew Ross Cc: gcc-bugs@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-7.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Thu, Jul 20, 2023 at 7:47=E2=80=AFAM Drew Ross via Gcc-bugs wrote: > > PR middle-end/101955 > > gcc/ChangeLog: > > * match.pd (x << c) >> c -> -(x & 1): New simplification. > > gcc/testsuite/ChangeLog: > > * gcc.dg/pr101955.c: New test. > --- > gcc/match.pd | 9 +++++ > gcc/testsuite/gcc.dg/pr101955.c | 62 +++++++++++++++++++++++++++++++++ > 2 files changed, 71 insertions(+) > create mode 100644 gcc/testsuite/gcc.dg/pr101955.c > > diff --git a/gcc/match.pd b/gcc/match.pd > index 8543f777a28..bf63652e80f 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -3766,6 +3766,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > && (wi::ltu_p (wi::to_wide (@1), element_precision (type)))) > (bit_and @0 (rshift { build_minus_one_cst (type); } @1)))) > > +/* Optimize (X << C) >> C where C =3D precision(type) - 1 and X is signe= d > + into -(X & 1). */ > +(simplify > + (rshift (lshift @0 INTEGER_CST@1) @@1) > + (if (ANY_INTEGRAL_TYPE_P (type) > + && !TYPE_UNSIGNED (type) > + && wi::eq_p (wi::to_wide (@1), element_precision (type) - 1)) > + (negate (bit_and @0 { build_one_cst (type); })))) I think the lshift does not need to be in the same type but could be in the unsigned type so you should add a nop_convert? operand there That is: (simplify (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1) ... (negate (bit_and (convert @0) { build_one_cst (type); })))) Note I see you use INTEGER_CST and then ANY_INTEGRAL_TYPE_P. If you are going to support vectors, you should use uniform_integer_cst_p instead of INTEGER_CST and then use uniform_integer_cst_p to get the constant. Thanks, Andrew > + > /* Optimize x >> x into 0 */ > (simplify > (rshift @0 @0) > diff --git a/gcc/testsuite/gcc.dg/pr101955.c b/gcc/testsuite/gcc.dg/pr101= 955.c > new file mode 100644 > index 00000000000..0e233269e21 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr101955.c > @@ -0,0 +1,62 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-dse1 -Wno-psabi" } */ > + > +typedef int v4si __attribute__((vector_size(16))); > + > +__attribute__((noipa)) int > +t1 (int x) > +{ > + return (x << 31) >> 31; > +} > + > +__attribute__((noipa)) int > +t2 (int x) > +{ > + int y =3D x << 31; > + int z =3D y >> 31; > + return z; > +} > + > +__attribute__((noipa)) int > +t3 (int x) > +{ > + int w =3D 31; > + int y =3D x << w; > + int z =3D y >> w; > + return z; > +} > + > +__attribute__((noipa)) long long > +t4 (long long x) > +{ > + return (x << 63) >> 63; > +} > + > +__attribute__((noipa)) long long > +t5 (long long x) > +{ > + long long y =3D x << 63; > + long long z =3D y >> 63; > + return z; > +} > + > +__attribute__((noipa)) long long > +t6 (long long x) > +{ > + int w =3D 63; > + long long y =3D x << w; > + long long z =3D y >> w; > + return z; > +} > + > +__attribute__((noipa)) v4si > +t7 (v4si x) > +{ > + return (x << 31) >> 31; > +} > + > +/* { dg-final { scan-tree-dump-not " >> " "dse1" } } */ > +/* { dg-final { scan-tree-dump-not " << " "dse1" } } */ > +/* { dg-final { scan-tree-dump-times " -" 7 "dse1" } } */ > +/* { dg-final { scan-tree-dump-times " & " 7 "dse1" } } */ > + > -- > 2.39.3 >