From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by sourceware.org (Postfix) with ESMTPS id BAFFF3858D33 for ; Fri, 28 Jul 2023 06:34:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BAFFF3858D33 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-pl1-x636.google.com with SMTP id d9443c01a7336-1bbc64f9a91so14329125ad.0 for ; Thu, 27 Jul 2023 23:34:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690526064; x=1691130864; 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=wCLO/jmQLddPLVhHJPMxIlY3uUdv59E4ZoL2YLygIE8=; b=hNJxbMazIRWlWhzmtbYMb8C4TMgWNUUsGfISJ0N1dhJn+2oQGGI8padO4/440YGTep 2xPlb1rlPsQ5U7NZrOhKOwwL392H0qwohtyvcZiNNsbe6Tjt1wgLvwxojAAVrfAnu7tp Uo717hCIp/rilIu69+7/gEJHRM4Jrbg41M0bjzG9dee0cLAEGp+DUMONTvLE+QbgP5E5 PJNVqcHW46MjXJ014LxalityYmhOxbqAkZ9ftTZiKWuHXzVrpqRaLlNyfZ+lgmLv09zf aQ9vgQxg2Ov+5turUeQkpvW4aGThXCCvJTTcHfFN/BNIOYbOD3mJb+VL7skjUDllSWeE DUAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690526064; x=1691130864; 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=wCLO/jmQLddPLVhHJPMxIlY3uUdv59E4ZoL2YLygIE8=; b=L9anK69T+MRUUjlS2E4ObW6lLY1ERT5yfSFxeVTGpc9vtJmjkC1ZUHe0N84wwU1KIj acOS/XXGrHKEQyxjjv34qxHocphIkQg2dWpSP63XpKm8Bu5GanBnU9KQjlqPXBpP2ger qFJGU4X5Mz9SKUkIF7rfiWpuOWJEUALzsMZm2FOO79XujVq7KkZHFpiAiOh0W4dir2XD WWQj7tgO7wG8jcxtELyLtlB8e23hFjuJZiaOUEo7q7zVFElIS1q3VFiBSqqBUCpF4+Hl E06RGDK9932rOCmPE4PUhmHrZ24B/Dj9J6bRoDXGLq9hGe1utGaOUyNRqfBOqYs/UMyv d4cw== X-Gm-Message-State: ABy/qLahrIG30GPec9/XOzX8UdoiEqxSo6dTc5i6BBFIfBaKEiCSB8T3 1kZ0YPLHJJgpL91MKNMnWYZSaDI2d5dS4GB5+VM= X-Google-Smtp-Source: APBJJlELfYpPx3a0dKUFGQHfWKWoKw7NFPgrQQJq4m5RWjeC763uHmpM50LdmisvzcS+TlDM6JCQkTb1dBybaRfhXUo= X-Received: by 2002:a17:902:c255:b0:1bb:a522:909a with SMTP id 21-20020a170902c25500b001bba522909amr953164plg.37.1690526063565; Thu, 27 Jul 2023 23:34:23 -0700 (PDT) MIME-Version: 1.0 References: <20230722232649.1617746-1-apinski@marvell.com> In-Reply-To: From: Andrew Pinski Date: Thu, 27 Jul 2023 23:34:11 -0700 Message-ID: Subject: Re: [PATCH] Fix 100864: `(a&!b) | b` is not opimized to `a | b` for comparisons 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.7 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 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. 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 sometim= es 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/g= cc.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 (volatile= ). */ > > +/* Each operator should show up twice > > + (except for `!=3D` which shows up 2*6 (each tester) + 2 (the 2 loop= s) extra =3D 16). */ > > +/* bit_ior_expr will show up for each operator twice (non-volatile and= 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 > >