From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by sourceware.org (Postfix) with ESMTPS id 4E3EF3858CDB for ; Thu, 20 Jul 2023 18:08:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4E3EF3858CDB 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-pj1-x102a.google.com with SMTP id 98e67ed59e1d1-262cc036fa4so524818a91.3 for ; Thu, 20 Jul 2023 11:08:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689876495; x=1690481295; 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=0fc+fgY4hwi7mYCsp7DxknetKzjERGjgEK/Y9K8+gwI=; b=pnZAgdvtO1G57Mj5N/+PKv3Bn+qROHjY6xBc7rrufyAKC94pJt25xWKvIbhSwpT3tr 3tj3qhAghuMklqttmtouIz9pi+r0RM2QnKwzCMoCNHOeTfstrE1j4sjN8is61yyMX//J aXmk6tfpWP73ngI/B+R00UcWoE5o3LHFPnYIiJH2RE0+bHOFLwrsc2Abwunvil0DqSOk GVvEcpvb2LGnBfnn3e4uWSCmoOZboAzLMIh/88nEcyC4abnPghQtE30TYDUzf9PKYBJb VNKAOsjdB4m/wbRpCrJlfNeICGI40nqi7fnHP2U1h0eGQKd4fwg/0+iVzicovVcUoTrk sftg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689876495; x=1690481295; 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=0fc+fgY4hwi7mYCsp7DxknetKzjERGjgEK/Y9K8+gwI=; b=WkSnQlVUmDC426BDG+g7ug2xVRy7jn+Pi7a9fdGdrzAyt83hSF8J6FtmO/TZZWw9Cy RaqMDwZ7NNiWTWFaUso2odlyC09B/ZXAwHjVryxzW0ijacxdgGH/K1tmO+4Q8X18GOH7 62LpIACRJyURa3trWBlIPKrZDrx1p/IbWUsKjURZpnObKWIA/Y4v4vWNTb6wib+H1KR8 AbSTHOQF3B5wIghsZb90aMAwYJN8ks5R10yCZMfh/Ax4FR5NcJOQ1pUWWaedCu37yz+h 5g1nZntrZ2rkOBvstkP61kk79j2qZScfruPyBSiFjIDsSwThcvfqReKsGdjlXjP2dEgY 9/CA== X-Gm-Message-State: ABy/qLbqn0WkJ29MCmSx1Jb+SThvszOTK7JwIiRC2NBmotYiaAVqNopR EFzCLc+KJSQdwjwszNJII64z/r+6L990A0wG47k= X-Google-Smtp-Source: APBJJlEo9hepba65cR2pu7OJ2JbkFab5/ANkCYrsFOHiX+alHvuA50h+E9DjfyXGgjNYJq9lppkI3iDzI+NigqUUuS4= X-Received: by 2002:a17:90a:c20b:b0:267:d9f1:c67 with SMTP id e11-20020a17090ac20b00b00267d9f10c67mr117070pjt.3.1689876494965; Thu, 20 Jul 2023 11:08:14 -0700 (PDT) MIME-Version: 1.0 References: <20230720144711.60975-1-drross@redhat.com> In-Reply-To: From: Andrew Pinski Date: Thu, 20 Jul 2023 11:08:03 -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.8 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 8:17=E2=80=AFAM Andrew Pinski w= rote: > > 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 sig= ned > > + 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. Note I just noticed you sent the patch to gcc-bugs@, it really should go to gcc-patches@ . Thanks, Andrew > > 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/pr1= 01955.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 > >