From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lj1-x22e.google.com (mail-lj1-x22e.google.com [IPv6:2a00:1450:4864:20::22e]) by sourceware.org (Postfix) with ESMTPS id 44AC83858D1E for ; Fri, 28 Jul 2023 06:40:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 44AC83858D1E 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-lj1-x22e.google.com with SMTP id 38308e7fff4ca-2b9b9f0387dso27469611fa.0 for ; Thu, 27 Jul 2023 23:40:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690526436; x=1691131236; 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=e/gkvkRoqHqXLKm2kJXvuA5w7AqPLkCZqNqxmGeFvyk=; b=QuI+teBK/rMf63yG6I8+yEjee4R2Cd+AbdaUIFoL5Mw39dMOWr6dYTL+uD4QLiVhln 3lYhxZq3rclkvo3B7TU5+RheF+NmVErrdtehq6UrVWTWLAh0555BhYiZu5eGafl77als Tksdh0EhuGlYYwMMguwaNFC1et8Jpoi6ehKS+UVUEVK2UYikZtjxVxjZasBQ81oInGPi nTiTKzb+DLc+gdPi2YIOzpW91RPNQ5fcYM6oTxaQJmg0DB/FMVKhBGVBaeXqWss/Xf6y z1s6jiK3gjmTqCw1MiYdgzDXQ/oHU6IiSYGFUcTPf4csPMMmqTclnU5XLZdA6FtezdRF Ofjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690526436; x=1691131236; 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=e/gkvkRoqHqXLKm2kJXvuA5w7AqPLkCZqNqxmGeFvyk=; b=SGZq0OYpmbw7SFI7Bx6TEgcITZqlDCuhph+hwMQIzLDYFaw1zQVKf83V2u6FZgvaTT lhjyvDGsXu7+AEIalHHDRnjSBGxwedNGCxfPi0gEL5zXOirLdLU8jg/ewuJSQ5U3PDR6 FjzRmMCkdc3z+58Y6kF7fH1YcUbQdbRzkiT2+x3x/zUTOAlefxUco6WSHiwRhvKWHSVX Rk5DTifdDAxswdnB1K14ONFxItnsxFSryJYqDRtLMEI6oilaBu2LkzQj7Tsi08BT3R7j undHSHE8s7lTrvpQFv0LRxvd0ov1RKVFmwQdyccNGxE4hHDCQHfHcYAv4ZHYnyOWdWt+ MvAA== X-Gm-Message-State: ABy/qLbjVdOTsxQRHkkpOpMTy54rzomPVPggikOw/AqNa73Zhykhu2Tb Owj1Ipty/zM3rSLi8LWeFsEXFQ5VwqaRNcgv0P4= X-Google-Smtp-Source: APBJJlHoYrmpTjwe2TnS/s9n6WdbAhwtPCcuB+x1p4XRF1YQpFc2f438JNb4aPRWn1ElQaN4zHqd03O5P/DUWHCspag= X-Received: by 2002:a2e:9c06:0:b0:2b6:cf6f:159e with SMTP id s6-20020a2e9c06000000b002b6cf6f159emr890342lji.44.1690526435767; Thu, 27 Jul 2023 23:40:35 -0700 (PDT) MIME-Version: 1.0 References: <20230722232649.1617746-1-apinski@marvell.com> In-Reply-To: From: Richard Biener Date: Fri, 28 Jul 2023 08:40:23 +0200 Message-ID: Subject: Re: [PATCH] Fix 100864: `(a&!b) | b` is not opimized to `a | b` for comparisons To: Andrew Pinski 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,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 Fri, Jul 28, 2023 at 8:34=E2=80=AFAM Andrew Pinski w= rote: > > On Sun, Jul 23, 2023 at 1:39=E2=80=AFAM Richard Biener via Gcc-patches > wrote: > > > > > > > > > Am 23.07.2023 um 01:27 schrieb Andrew Pinski via Gcc-patches : > > > > > > =EF=BB=BFThis adds a special case of the `(a&~b) | b` pattern where > > > `b` and `~b` are comparisons. > > > > > > OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. > > > > Don=E2=80=99t we have an existing match for inversion s we could amend? > > We don't currently but I might be able to pattern the function off of > what was similarly done for bitwise_equal_p . > I noticed the patch which added bitwise_equal_p even could benefit > from this similar thing. OK, I thought of logical_inverted_value but that isn't a 1:1 match here. Richard. > Thanks, > Andrew > > > > > > gcc/ChangeLog: > > > > > > PR tree-optimization/100864 > > > * match.pd ((~x & y) | x -> x | y): Add comparison variant. > > > > > > gcc/testsuite/ChangeLog: > > > > > > * gcc.dg/tree-ssa/bitops-3.c: New test. > > > --- > > > gcc/match.pd | 17 +++++- > > > gcc/testsuite/gcc.dg/tree-ssa/bitops-3.c | 67 +++++++++++++++++++++++= + > > > 2 files changed, 83 insertions(+), 1 deletion(-) > > > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bitops-3.c > > > > > > diff --git a/gcc/match.pd b/gcc/match.pd > > > index bfd15d6cd4a..dd4a2df537d 100644 > > > --- a/gcc/match.pd > > > +++ b/gcc/match.pd > > > @@ -1928,7 +1928,22 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > > > /* (~x & y) | x -> x | y */ > > > (simplify > > > (bitop:c (rbitop:c (bit_not @0) @1) @0) > > > - (bitop @0 @1))) > > > + (bitop @0 @1)) > > > + /* Similar but for comparisons which have been inverted already, > > > + Note it is hard to simulate the inverted tcc_comparison due > > > + NaNs; That is =3D=3D and !=3D are sometimes inversions and somet= imes not. > > > + So a double for loop is needed and then compare the inverse code > > > + with the result of invert_tree_comparison is needed. > > > + This works fine for vector compares as -1 and 0 are bitwise > > > + inverses. */ > > > + (for cmp (tcc_comparison) > > > + (for icmp (tcc_comparison) > > > + (simplify > > > + (bitop:c (rbitop:c (icmp @0 @1) @2) (cmp@3 @0 @1)) > > > + (with { enum tree_code ic =3D invert_tree_comparison > > > + (cmp, HONOR_NANS (@0)); } > > > + (if (ic =3D=3D icmp) > > > + (bitop @3 @2))))))) > > > > > > /* ((x | y) & z) | x -> (z & y) | x */ > > > (simplify > > > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bitops-3.c b/gcc/testsuite= /gcc.dg/tree-ssa/bitops-3.c > > > new file mode 100644 > > > index 00000000000..68fff4edce9 > > > --- /dev/null > > > +++ b/gcc/testsuite/gcc.dg/tree-ssa/bitops-3.c > > > @@ -0,0 +1,67 @@ > > > +/* PR tree-optimization/100864 */ > > > + > > > +/* { dg-do run } */ > > > +/* { dg-options "-O1 -fdump-tree-optimized-raw" } */ > > > + > > > +#define op_ne !=3D > > > +#define op_eq =3D=3D > > > +#define op_lt < > > > +#define op_le <=3D > > > +#define op_gt > > > > +#define op_ge >=3D > > > + > > > +#define operators(t) \ > > > +t(ne) \ > > > +t(eq) \ > > > +t(lt) \ > > > +t(le) \ > > > +t(gt) \ > > > +t(ge) > > > + > > > +#define cmpfunc(v, op) \ > > > +__attribute__((noipa)) \ > > > +_Bool func_##op##_##v(v int a, v int b, v _Bool e) \ > > > +{ \ > > > + v _Bool c =3D (a op_##op b); \ > > > + v _Bool d =3D !c; \ > > > + return (e & d) | c; \ > > > +} > > > + > > > +#define cmp_funcs(op) \ > > > +cmpfunc(, op) \ > > > +cmpfunc(volatile , op) > > > + > > > +operators(cmp_funcs) > > > + > > > +#define test(op) \ > > > +if (func_##op##_ (a, b, e) !=3D func_##op##_volatile (a, b, e)) \ > > > + __builtin_abort(); > > > + > > > +int main() > > > +{ > > > + for(int a =3D -3; a <=3D 3; a++) > > > + for(int b =3D -3; b <=3D 3; b++) > > > + { > > > + _Bool e =3D 0; > > > + operators(test) > > > + e =3D 1; > > > + operators(test) > > > + } > > > + return 0; > > > +} > > > + > > > +/* Check to make sure we optimize `(a&!b) | b` -> `a | b`. */ > > > +/* There are 6 different comparison operators testing here. */ > > > +/* bit_not_expr and bit_and_expr should show up for each one (volati= le). */ > > > +/* Each operator should show up twice > > > + (except for `!=3D` which shows up 2*6 (each tester) + 2 (the 2 lo= ops) extra =3D 16). */ > > > +/* bit_ior_expr will show up for each operator twice (non-volatile a= nd volatile). */ > > > +/* { dg-final { scan-tree-dump-times "ne_expr," 16 "optimized"}= } */ > > > +/* { dg-final { scan-tree-dump-times "eq_expr," 2 "optimized"}= } */ > > > +/* { dg-final { scan-tree-dump-times "lt_expr," 2 "optimized"}= } */ > > > +/* { dg-final { scan-tree-dump-times "le_expr," 2 "optimized"}= } */ > > > +/* { dg-final { scan-tree-dump-times "gt_expr," 2 "optimized"}= } */ > > > +/* { dg-final { scan-tree-dump-times "ge_expr," 2 "optimized"}= } */ > > > +/* { dg-final { scan-tree-dump-times "bit_not_expr," 6 "optimized"}= } */ > > > +/* { dg-final { scan-tree-dump-times "bit_and_expr," 6 "optimized"}= } */ > > > +/* { dg-final { scan-tree-dump-times "bit_ior_expr," 12 "optimized"}= } */ > > > \ No newline at end of file > > > -- > > > 2.31.1 > > >