From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by sourceware.org (Postfix) with ESMTPS id 134FE3858D32 for ; Mon, 18 Sep 2023 23:55:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 134FE3858D32 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-wr1-x431.google.com with SMTP id ffacd0b85a97d-31ff1f3cde5so3646990f8f.2 for ; Mon, 18 Sep 2023 16:55:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695081337; x=1695686137; 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=0SyMCqT54Z7lXw/kLbVTbeDqM2iegHxsTLeTBIO9fCc=; b=nUjLS1pF2vZ1U7E3Y30ainBCND8nVdXOOU2K76rtYEttAvcMp2+gGAxJaWRcd0jzpV nwSnH+pTNqJRRJUg6r04Nc9hTz72QtZ1DCXMR/pMJhJDcJ/guUk8NstVCzHRz0u7OjgT EGwtgF2JAw4qL4n5V01u8hhU+za22FR+Bp2lN0OH6RjoZOXiZH1CQ75YMZf5ua9Gg1ta hyL3GJkecimPOFgHEwMHYkC98Hz/uuO//pQRjxGCs1Bta5yq3N6cw6osRnUtnE9eni4t HZlryeLIcintjIlXRYxvoxxusyPfnj3dkgMplThJuIMMwWjHVACDy1TAJyb73vfKsung OCpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695081337; x=1695686137; 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=0SyMCqT54Z7lXw/kLbVTbeDqM2iegHxsTLeTBIO9fCc=; b=CvVb4wOl2GWcio9l0FN/5e0O4MEl9g3/njZBbTJ0RwJZ+OeAfX8b0MBqMlacMOjflr 0lvzBCJl0V9rTKl+PvWegEgwO4Nq3veYwtbDFSDAFv/et5aa1x2yR8h5xw9Vl2mvdSTS ZCvTme/PjYbOOeQ7x8ifbGbQFZZPQfXhfb4Ky9ATOoKcL83lCG1itk1HgKvvv5/hdT/B 6LVvlyVg/tmVR/C9U1y1F/lEa1WDg/HjmHBXV61EyAA24O1I/2slIhyJbCmgPBkhovI3 D7BZ3rZEizdL1bmhFKb7ddXpoAmxWGhN51rvvOTgNIe/UrmuV1zZ0/1uQ/8N4KgkVcd1 lvXg== X-Gm-Message-State: AOJu0YwMMjzC33UnanOFOfw5sD9JVEykKp1loHsRYU9rkNATFhnD5QvG a2KvJSFglFhGYAeor85kX5qvxV1bTInrC4nPsS8= X-Google-Smtp-Source: AGHT+IHQYsCf1lJz7rFOqb6R71Dtvnb48sOhvbzpDITVZ8sYG/Uas0n3Ook78wVQHV2gGk6fPo+otLHCd6jQp6Wi1x8= X-Received: by 2002:a5d:4152:0:b0:319:77dd:61f9 with SMTP id c18-20020a5d4152000000b0031977dd61f9mr8261980wrq.35.1695081337256; Mon, 18 Sep 2023 16:55:37 -0700 (PDT) MIME-Version: 1.0 References: <20230916054920.1653864-1-apinski@marvell.com> In-Reply-To: From: Andrew Pinski Date: Mon, 18 Sep 2023 16:55:24 -0700 Message-ID: Subject: Re: [PATCH] MATCH: Add simplifications for `(a * zero_one) ==/!= CST` To: Richard Biener Cc: Andrew Pinski , gcc-patches@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 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 Mon, Sep 18, 2023 at 12:09=E2=80=AFAM Richard Biener via Gcc-patches wrote: > > On Sat, Sep 16, 2023 at 7:50=E2=80=AFAM Andrew Pinski via Gcc-patches > wrote: > > > > Transforming `(a * b@[0,1]) !=3D 0` into `((cast)b) & a !=3D 0` > > that isn't strictly a simplification (one more op), and your > alternate transform is even worse in this regard. Right, I agree here. I was trying to workaround a ranger issue (see below). > > > will produce better code as a lot of the time b is defined > > by a comparison. > > what if not? How does it simplify then? > > > Also since canonicalize `a & -zero_one` into `a * zero_one` we > > start to lose information when doing comparisons against 0. > > In the case of PR 110992, we lose that `a !=3D 0` on the branch > > How so? Ranger should be happy with both forms, no? Ranger does not handle going backwards on the multiply case; only on the bit_and case. I tried figuring out how to understand that works but I got lost in the ranger code. Maybe Andrew or Aldy could look into figuring out how to improve ranger here. Thanks, Andrew > > > and then don't do a jump threading like we should. > > > > OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. > > > > PR tree-optimization/110992 > > > > gcc/ChangeLog: > > > > * match.pd (`a * zero_one !=3D/=3D=3D CST`): New pattern. > > > > gcc/testsuite/ChangeLog: > > > > * gcc.dg/tree-ssa/vrp116.c: Update test to avoid the > > extra comparison. > > * gcc.c-torture/execute/pr110992-1.c: New test. > > * gcc.dg/tree-ssa/pr110992-1.c: New test. > > * gcc.dg/tree-ssa/pr110992-2.c: New test. > > --- > > gcc/match.pd | 15 +++++++ > > .../gcc.c-torture/execute/pr110992-1.c | 43 +++++++++++++++++++ > > gcc/testsuite/gcc.dg/tree-ssa/pr110992-1.c | 21 +++++++++ > > gcc/testsuite/gcc.dg/tree-ssa/pr110992-2.c | 17 ++++++++ > > gcc/testsuite/gcc.dg/tree-ssa/vrp116.c | 2 +- > > 5 files changed, 97 insertions(+), 1 deletion(-) > > create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr110992-1.c > > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110992-1.c > > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110992-2.c > > > > diff --git a/gcc/match.pd b/gcc/match.pd > > index 39c9c81966a..97405e6a5c3 100644 > > --- a/gcc/match.pd > > +++ b/gcc/match.pd > > @@ -2197,6 +2197,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > > (if (INTEGRAL_TYPE_P (type)) > > (bit_and @0 @1))) > > > > +/* (a * b@[0,1]) =3D=3D CST > > + -> > > + CST =3D=3D 0 ? (a =3D=3D CST | b =3D=3D 0) : (a =3D=3D CST & b = !=3D 0) > > + (a * b@[0,1]) !=3D CST > > + -> > > + CST !=3D 0 ? (a !=3D CST | b =3D=3D 0) : (a !=3D CST & b !=3D 0= ) */ > > +(for cmp (ne eq) > > + (simplify > > + (cmp (mult:cs @0 zero_one_valued_p@1) INTEGER_CST@2) > > + (if ((cmp =3D=3D EQ_EXPR) ^ (wi::to_wide (@2) !=3D 0)) > > + (bit_ior > > + (cmp @0 @2) > > + (convert (bit_xor @1 { build_one_cst (TREE_TYPE (@1)); }))) > > + (bit_and (cmp @0 @2) (convert @1))))) > > + > > (for cmp (tcc_comparison) > > icmp (inverted_tcc_comparison) > > /* Fold (((a < b) & c) | ((a >=3D b) & d)) into (a < b ? c : d) & 1. = */ > > diff --git a/gcc/testsuite/gcc.c-torture/execute/pr110992-1.c b/gcc/tes= tsuite/gcc.c-torture/execute/pr110992-1.c > > new file mode 100644 > > index 00000000000..edb7eb75ef2 > > --- /dev/null > > +++ b/gcc/testsuite/gcc.c-torture/execute/pr110992-1.c > > @@ -0,0 +1,43 @@ > > +#define CST 5 > > +#define OP !=3D > > +#define op_eq =3D=3D > > +#define op_ne !=3D > > + > > +#define function(vol,op, cst) \ > > +__attribute__((noipa)) \ > > +_Bool func_##op##_##cst##_##vol(vol int a, vol _Bool b) \ > > +{ \ > > + vol int d =3D (a * b); \ > > + return d op_##op cst; \ > > +} > > + > > +#define funcdefs(op,cst) \ > > +function(,op,cst) \ > > +function(volatile,op,cst) > > + > > +#define funcs(f) \ > > +f(eq,0) \ > > +f(eq,1) \ > > +f(eq,5) \ > > +f(ne,0) \ > > +f(ne,1) \ > > +f(ne,5) > > + > > +funcs(funcdefs) > > + > > +#define test(op,cst) \ > > +do { \ > > + if(func_##op##_##cst##_(a,b) !=3D func_##op##_##cst##_volatile(a,b))\ > > + __builtin_abort(); \ > > +} while(0); > > + > > +int main(void) > > +{ > > + for(int a =3D -10; a <=3D 10; a++) > > + { > > + _Bool b =3D 0; > > + funcs(test) > > + b =3D 1; > > + funcs(test) > > + } > > +} > > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110992-1.c b/gcc/testsuite= /gcc.dg/tree-ssa/pr110992-1.c > > new file mode 100644 > > index 00000000000..825fd63f84c > > --- /dev/null > > +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110992-1.c > > @@ -0,0 +1,21 @@ > > +/* { dg-do compile } */ > > +/* { dg-options "-O3 -fdump-tree-optimized" } */ > > +static unsigned b; > > +static short c =3D 4; > > +void foo(void); > > +static short(a)(short d, short g) { return d * g; } > > +void e(); > > +static char f() { > > + b =3D 0; > > + return 0; > > +} > > +int main() { > > + int h =3D b; > > + if ((short)(a(c && e, 65535) & h)) { > > + foo(); > > + h || f(); > > + } > > +} > > + > > +/* There should be no calls to foo left. */ > > +/* { dg-final { scan-tree-dump-not " foo " "optimized" } } */ > > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110992-2.c b/gcc/testsuite= /gcc.dg/tree-ssa/pr110992-2.c > > new file mode 100644 > > index 00000000000..6082949a218 > > --- /dev/null > > +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110992-2.c > > @@ -0,0 +1,17 @@ > > +/* { dg-do compile } */ > > +/* { dg-options "-O3 -fdump-tree-optimized" } */ > > +static unsigned b; > > +static short c =3D 4; > > +void foo(void); > > +int main() { > > + int h =3D b; > > + int d =3D c !=3D 0; > > + if (h*d) { > > + foo(); > > + if (!h) b =3D 20; > > + } > > +} > > + > > + > > +/* There should be no calls to foo left. */ > > +/* { dg-final { scan-tree-dump-not " foo " "optimized" } } */ > > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp116.c b/gcc/testsuite/gcc= .dg/tree-ssa/vrp116.c > > index 9e68a774aee..16b31e320a0 100644 > > --- a/gcc/testsuite/gcc.dg/tree-ssa/vrp116.c > > +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp116.c > > @@ -6,7 +6,7 @@ f (int m1, int m2, int c) > > { > > int d =3D m1 > m2; > > int e =3D d * c; > > - return e ? m1 : m2; > > + return e; > > } > > > > /* { dg-final { scan-tree-dump-times "\\? c_\[0-9\]\\(D\\) : 0" 1 "opt= imized" } } */ > > -- > > 2.31.1 > >