From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) by sourceware.org (Postfix) with ESMTPS id 71E4C3858D32 for ; Mon, 18 Sep 2023 07:37:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 71E4C3858D32 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-lf1-x12d.google.com with SMTP id 2adb3069b0e04-50308217223so2033995e87.3 for ; Mon, 18 Sep 2023 00:37:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695022635; x=1695627435; darn=gcc.gnu.org; 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=f/KF7cC+nSBl7QaROYhOrmj6HOqNzAQgC16zHKIo8w4=; b=OpDXnVprqmChTnEQHCfLLnXDMvs2VddxCJ0Vpmic3oBkiGXnnKtq2GiJXVEzGrwuFi g4443C+G2TaparrZhn0vm0LadMQYGTjAxZFxdlAOpYwWzu0EICf/bUfIuYRY0j4HuTpb DKYRNBN4nYNo2QfOxY5Wu9d9h8larTfIJl85TocWUEQC1opCYBZZby7KbxYQzo6PtCf2 QZWVDInVTmW6VdjMPhBmf5HoKU1EOPd5iBV1cRql0vz8pQ7tOoSgUKUp8sGjokm1eM7o UVq0o+Eo81dcmYi1HUm3Co3luRKqJcr+KfMhOXXhsUHeW+/Me3qugwk/Q7HwUVJfC4Aj o3wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695022635; x=1695627435; 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=f/KF7cC+nSBl7QaROYhOrmj6HOqNzAQgC16zHKIo8w4=; b=RlZpyyPQjXFF3JQod+FyoXUyM/ORq7IFzA2Onovm0+nkz1BxQB6zisANEUf8SIIR7Q JaVLWCtaIQnhMEvSSMKS/0w9JOJ4y3eLylq9gu4EnK7V6AQwqlNl5BX76gceZPgnL96X A1sng7TwE1WMTdY+z2O+4PpJgn6b5tlYSrR43pjImS+Hg/8rt5bdkHb9ub54oxYbIQX4 Ru+RuyMVqIsrzrOetspyh1YpHLbctx79f8CFkpP5PVcHupaGb6PYmGYhB7k1vwVX5d3d Je9Rjlu+U2OjFbW1KSbIazq/nGrjCUFu3XQ1WxWMFQISp49mFgH4w1LC3JUfznd4gLxp sR7g== X-Gm-Message-State: AOJu0YyWC/fYk4XZcs0LfdIP5Kbbr2e4OYUDwuqQCon1LrX/LsK3eJEc phAXFV9z27n4TOjWnNA3AHccYiPgyaCxUHH0fCyVF2rZ X-Google-Smtp-Source: AGHT+IEkgXYcN7npNoQJkciKMteEsikxw9LhGARD7A8nyGtgPv8XdAH5H4bA7GUsLYtsBAnNl+phA4/KYLkUVKC3OMo= X-Received: by 2002:ac2:5f99:0:b0:503:970:4dbf with SMTP id r25-20020ac25f99000000b0050309704dbfmr2787786lfe.15.1695022634646; Mon, 18 Sep 2023 00:37:14 -0700 (PDT) MIME-Version: 1.0 References: <20230916155924.1679335-1-apinski@marvell.com> In-Reply-To: <20230916155924.1679335-1-apinski@marvell.com> From: Richard Biener Date: Mon, 18 Sep 2023 09:37:02 +0200 Message-ID: Subject: Re: [PATCH] MATCH: Add simplifications of `(a == CST) & a` To: Andrew Pinski Cc: gcc-patches@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-7.5 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 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 Sat, Sep 16, 2023 at 6:00=E2=80=AFPM Andrew Pinski via Gcc-patches wrote: > > `(a =3D=3D CST) & a` can be either simplified to simplying `a =3D=3D CST` > or 0 depending on the first bit of the CST. > This is an extension of the already pattern of `X & !X` and allows > us to remove the 2 xfails on gcc.dg/binop-notand1a.c and gcc.dg/binop-not= and4a.c. > > OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. OK. > PR tree-optimization/111431 > > gcc/ChangeLog: > > * match.pd (`(a =3D=3D CST) & a`): New pattern. > > gcc/testsuite/ChangeLog: > > * gcc.dg/binop-notand1a.c: Remove xfail. > * gcc.dg/binop-notand4a.c: Likewise. > * gcc.c-torture/execute/pr111431-1.c: New test. > * gcc.dg/binop-andeq1.c: New test. > * gcc.dg/binop-andeq2.c: New test. > * gcc.dg/binop-notand7.c: New test. > * gcc.dg/binop-notand7a.c: New test. > --- > gcc/match.pd | 8 ++++ > .../gcc.c-torture/execute/pr111431-1.c | 39 +++++++++++++++++++ > gcc/testsuite/gcc.dg/binop-andeq1.c | 12 ++++++ > gcc/testsuite/gcc.dg/binop-andeq2.c | 14 +++++++ > gcc/testsuite/gcc.dg/binop-notand1a.c | 4 +- > gcc/testsuite/gcc.dg/binop-notand4a.c | 4 +- > gcc/testsuite/gcc.dg/binop-notand7.c | 12 ++++++ > gcc/testsuite/gcc.dg/binop-notand7a.c | 12 ++++++ > 8 files changed, 99 insertions(+), 6 deletions(-) > create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr111431-1.c > create mode 100644 gcc/testsuite/gcc.dg/binop-andeq1.c > create mode 100644 gcc/testsuite/gcc.dg/binop-andeq2.c > create mode 100644 gcc/testsuite/gcc.dg/binop-notand7.c > create mode 100644 gcc/testsuite/gcc.dg/binop-notand7a.c > > diff --git a/gcc/match.pd b/gcc/match.pd > index ebb50ee0581..65960a1701e 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -5172,6 +5172,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > ) > ) > > +/* `(a =3D=3D CST) & a` can be simplified to `0` or `(a =3D=3D CST)` dep= ending > + on the first bit of the CST. */ > +(simplify > + (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0)) > + (if ((wi::to_wide (@1) & 1) !=3D 0) > + @2 > + { build_zero_cst (type); })) > + > /* Optimize > # x_5 in range [cst1, cst2] where cst2 =3D cst1 + 1 > x_5 ? cstN ? cst4 : cst3 > diff --git a/gcc/testsuite/gcc.c-torture/execute/pr111431-1.c b/gcc/tests= uite/gcc.c-torture/execute/pr111431-1.c > new file mode 100644 > index 00000000000..a96dbadf2b5 > --- /dev/null > +++ b/gcc/testsuite/gcc.c-torture/execute/pr111431-1.c > @@ -0,0 +1,39 @@ > +int > +foo (int a) > +{ > + int b =3D a =3D=3D 0; > + return (a & b); > +} > + > +#define function(vol,cst) \ > +__attribute__((noipa)) \ > +_Bool func_##cst##_##vol(vol int a) \ > +{ \ > + vol int b =3D a =3D=3D cst; \ > + return (a & b); \ > +} > + > +#define funcdefs(cst) \ > +function(,cst) \ > +function(volatile,cst) > + > +#define funcs(f) \ > +f(0) \ > +f(1) \ > +f(5) > + > +funcs(funcdefs) > + > +#define test(cst) \ > +do { \ > + if(func_##cst##_(a) !=3D func_##cst##_volatile(a))\ > + __builtin_abort(); \ > +} while(0); > +int main(void) > +{ > + for(int a =3D -10; a <=3D 10; a++) > + { > + funcs(test) > + } > +} > + > diff --git a/gcc/testsuite/gcc.dg/binop-andeq1.c b/gcc/testsuite/gcc.dg/b= inop-andeq1.c > new file mode 100644 > index 00000000000..2a92b8f95df > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/binop-andeq1.c > @@ -0,0 +1,12 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-optimized" } */ > +/* PR tree-optimization/111431 */ > + > +int > +foo (int a) > +{ > + int b =3D a =3D=3D 2; > + return (a & b); > +} > + > +/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */ > diff --git a/gcc/testsuite/gcc.dg/binop-andeq2.c b/gcc/testsuite/gcc.dg/b= inop-andeq2.c > new file mode 100644 > index 00000000000..895262fc17e > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/binop-andeq2.c > @@ -0,0 +1,14 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-optimized" } */ > +/* PR tree-optimization/111431 */ > + > +int > +foo (int a) > +{ > + int b =3D a =3D=3D 1025; > + return (a & b); > +} > + > +/* { dg-final { scan-tree-dump-not "return 0" "optimized" } } */ > +/* { dg-final { scan-tree-dump-not " & " "optimized" } } */ > +/* { dg-final { scan-tree-dump-times " =3D=3D 1025;" 1 "optimized" } } = */ > diff --git a/gcc/testsuite/gcc.dg/binop-notand1a.c b/gcc/testsuite/gcc.dg= /binop-notand1a.c > index c7e932b2638..d94685eb4ce 100644 > --- a/gcc/testsuite/gcc.dg/binop-notand1a.c > +++ b/gcc/testsuite/gcc.dg/binop-notand1a.c > @@ -7,6 +7,4 @@ foo (char a, unsigned short b) > return (a & !a) | (b & !b); > } > > -/* As long as comparisons aren't boolified and casts from boolean-types > - aren't preserved, the folding of X & !X to zero fails. */ > -/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" { xfail *-= *-* } } } */ > +/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */ > diff --git a/gcc/testsuite/gcc.dg/binop-notand4a.c b/gcc/testsuite/gcc.dg= /binop-notand4a.c > index dce6a5c7eb5..bd9c7cce638 100644 > --- a/gcc/testsuite/gcc.dg/binop-notand4a.c > +++ b/gcc/testsuite/gcc.dg/binop-notand4a.c > @@ -7,6 +7,4 @@ foo (unsigned char a, _Bool b) > return (!a & a) | (b & !b); > } > > -/* As long as comparisons aren't boolified and casts from boolean-types > - aren't preserved, the folding of X & !X to zero fails. */ > -/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" { xfail *-= *-* } } } */ > +/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */ > diff --git a/gcc/testsuite/gcc.dg/binop-notand7.c b/gcc/testsuite/gcc.dg/= binop-notand7.c > new file mode 100644 > index 00000000000..c2bb6a0449d > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/binop-notand7.c > @@ -0,0 +1,12 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-optimized" } */ > +/* PR tree-optimization/111431 */ > + > +int > +foo (int a) > +{ > + int b =3D !a; > + return (a & b); > +} > + > +/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */ > diff --git a/gcc/testsuite/gcc.dg/binop-notand7a.c b/gcc/testsuite/gcc.dg= /binop-notand7a.c > new file mode 100644 > index 00000000000..dd50916da61 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/binop-notand7a.c > @@ -0,0 +1,12 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-optimized" } */ > +/* PR tree-optimization/111431 */ > + > +unsigned > +foo (int a) > +{ > + int b =3D !a; > + return (a & b); > +} > + > +/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */ > -- > 2.31.1 >