From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [IPv6:2a00:1450:4864:20::633]) by sourceware.org (Postfix) with ESMTPS id EBFAF3858D20 for ; Sun, 23 Jul 2023 08:38:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EBFAF3858D20 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-ej1-x633.google.com with SMTP id a640c23a62f3a-9922d6f003cso581849566b.0 for ; Sun, 23 Jul 2023 01:38:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690101532; x=1690706332; h=to:in-reply-to:cc:references:message-id:date:subject:mime-version :from:content-transfer-encoding:from:to:cc:subject:date:message-id :reply-to; bh=0+ua0sZnEJk5AkpxD/49+JhJw9K8+SjCyKPdz5q9GxI=; b=PwAdghFns/kNvz69TZCrpORj3WbVInGRxKq3xpWDojnkZuv9zmOzDaDPdaJ6PebJTw pnG9KWrK+dYNdk5ISkCjasbLp9lL7K3hA00w6vzNlmUBK96SWP8hNg1gTG3U/BgMkyFu zLWsbvaWYyx9S6pASR1hXKlKIEdGv085NrMo5NJ9FyUmI0ybZvmBoKmSHCNfTfGMD2QM uo1vl4H8MpZBZpUJFRz/MXkB/l1k/qHWXu8i0qKKGT5lx95Ba//Sg4NbuMAuyiXDtQ9+ YJ8vgNOkNPXlkIayFCuJdNliwVMY6FCxtjei0tzKOA65yZ0MMeuD1C28qXuImkv5DT6g utRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690101532; x=1690706332; h=to:in-reply-to:cc:references:message-id:date:subject:mime-version :from:content-transfer-encoding:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0+ua0sZnEJk5AkpxD/49+JhJw9K8+SjCyKPdz5q9GxI=; b=ifeniKT/VQI4KEprbBqdQQQwtQ4bGnEmc4cjjpPokwCj66FnpDc18eUW09IiCHZWf6 lCxf2lY8jr5jfAABPwoDG4wRV2WHoJWXbz8Ph8EMXyY1cgljnPjQAuAPDFCTLlZoRD2d jpEnUAiEJIpIieVoy2NAAHileshUix7dfCjeSCU2C5WrP5AN25GlVvLQOmVKEmhf0jcY 3QkMRN3Yg+uoA3N+CiUoYbGcLUjwwKC4mbprGcd/o9CbYPFmRA1RqbtEgGxyjZ4UJfty 2a8Khnl0KRrfo+B+v3WaDqKjl/ojEK+5bibv2McJYobwkg7MvI9Vu84PFey8tB2uveow Gfow== X-Gm-Message-State: ABy/qLbcz6xySMjk9cKLuSzvtgFOBSaH11ID9UE1s8fNtHqODpUJWlol 6ACQXmQERb6FqNvABGSUarX2PY2Izmc= X-Google-Smtp-Source: APBJJlEM9z1kqb6SemVBRW1m6/10mLS6G3WIlwRHfPbmejYjJoZCev0Pg4QBfrDlxopSPcaAP1mURQ== X-Received: by 2002:a17:906:1089:b0:965:6075:d100 with SMTP id u9-20020a170906108900b009656075d100mr6914302eju.39.1690101531976; Sun, 23 Jul 2023 01:38:51 -0700 (PDT) Received: from smtpclient.apple (dynamic-095-115-103-183.95.115.pool.telefonica.de. [95.115.103.183]) by smtp.gmail.com with ESMTPSA id h13-20020a170906854d00b0099290e2c163sm4741391ejy.204.2023.07.23.01.38.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 23 Jul 2023 01:38:50 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: Richard Biener Mime-Version: 1.0 (1.0) Subject: Re: [PATCH] Fix 100864: `(a&!b) | b` is not opimized to `a | b` for comparisons Date: Sun, 23 Jul 2023 10:38:40 +0200 Message-Id: References: <20230722232649.1617746-1-apinski@marvell.com> Cc: gcc-patches@gcc.gnu.org In-Reply-To: <20230722232649.1617746-1-apinski@marvell.com> To: Andrew Pinski X-Mailer: iPhone Mail (20F75) X-Spam-Status: No, score=-10.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,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: > Am 23.07.2023 um 01:27 schrieb Andrew Pinski via Gcc-patches : >=20 > =EF=BB=BFThis adds a special case of the `(a&~b) | b` pattern where > `b` and `~b` are comparisons. >=20 > 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? > gcc/ChangeLog: >=20 > PR tree-optimization/100864 > * match.pd ((~x & y) | x -> x | y): Add comparison variant. >=20 > gcc/testsuite/ChangeLog: >=20 > * 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 >=20 > 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 sometimes n= ot. > + 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))))))) >=20 > /* ((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(); > +=20 > +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 loops) e= xtra =3D 16). */ > +/* bit_ior_expr will show up for each operator twice (non-volatile and vo= latile). */ > +/* { 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 > --=20 > 2.31.1 >=20