From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x130.google.com (mail-lf1-x130.google.com [IPv6:2a00:1450:4864:20::130]) by sourceware.org (Postfix) with ESMTPS id 03D373858426 for ; Sun, 24 Sep 2023 06:35:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 03D373858426 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-x130.google.com with SMTP id 2adb3069b0e04-503397ee920so6926383e87.1 for ; Sat, 23 Sep 2023 23:35:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695537358; x=1696142158; 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=VuEPxsrJ9e9Ul++aKaPbFoHe3JPu8uTDNpN3j/OZl34=; b=Jcjer+2KreEsaeIpy+7Z4jnFfrzsfYoMgcA8Pil04LYw1K+YGJLZGhf2ZNvm4Cbo3V OthB0QaMV8cOPwL3oOlugIQeSjD6dw6+LnBX5y4Qr6MLIN7goBK9B5JZE7utRBtpr8rB 4b+xJGRxEv1J8IpiARlApCkJPYV8gTtWEpn7SLTrdf+ai+H1q1oz9eCg2jYu6msy60Rv V4A2/0VTtn2zGk9a9HM43U/tNBAdPaByI2I3hl+jt/Aqxh7eUfEjoJRcx57AyWDIUFVV hKZxk9PMWBBH+Cvph0bW7Nsg5tZodXFzE2arbMZ4Geka38VQETssJpr0xFiAJJ3WBloZ LFVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695537358; x=1696142158; 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=VuEPxsrJ9e9Ul++aKaPbFoHe3JPu8uTDNpN3j/OZl34=; b=X4qSCsLMAELW2XebZRm7JVt4bv2+z0sXnt6r8CR/yRvuH7mEGPuHqztBcsObksmvrM ZA/dlBtjrGhxZU5ZRbAtFgL12xW/p5lbpGmg2Ae6yPjWCAjZVZEziXoObKMINAOVvjoM nfwVFwj3uNJXLNV4H8cjw5dO1Nr6GZ5nnc9t9p5rGxHy8rFE9P6Cqx0rSXsCFBEmF3Nq oj3Xhq7xzuh1zsjoOxG4I+akTVyve5ceKi7R+/Zp9nUR+N9zIHLfZUFJRc0LnPzFNfnT BteMSh82GQVOtpSS65Qq6tEhhOGAm8VXvcFAhz2ShPhdtbky6hnzmKDY7U5SMoZjDT5+ 4f4w== X-Gm-Message-State: AOJu0YyhSsFAdACvP1Ho4xYhuK/2P5Wc+4OgwgSEzaki7KDo3K6YcBJt /0sVhfpso2sa84R2drciopgqs41uYM3ajVMzE6Q= X-Google-Smtp-Source: AGHT+IHzd6uPNpiW128UtFz6eNuronZ9uCjKAEwokWi9LmS0jCFsaOgs9PR+Nyi+0tB8DK2oD0AvhzDfSNkHP+T8xyE= X-Received: by 2002:a05:6512:104c:b0:502:9fce:b6e0 with SMTP id c12-20020a056512104c00b005029fceb6e0mr3611329lfb.28.1695537358168; Sat, 23 Sep 2023 23:35:58 -0700 (PDT) MIME-Version: 1.0 References: <20230923194921.2101021-1-apinski@marvell.com> In-Reply-To: <20230923194921.2101021-1-apinski@marvell.com> From: Richard Biener Date: Sun, 24 Sep 2023 08:35:45 +0200 Message-ID: Subject: Re: [PATCH] MATCH: Add `(X & ~Y) & Y` and `(X | ~Y) | Y` 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.3 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 23, 2023 at 9:50=E2=80=AFPM Andrew Pinski = wrote: > > Even though this gets optimized by reassociation, catching it more often > will always be better. > > Note the reason why I didn't add `(X ^ ~Y) ^ Y` is that it gets caught > by prefering `~(X ^ Y)` to `(X ^ ~Y)` which then it is caught by the > the pattern for `(X ^ Y) ^ Y` already. OK > PR tree-optimization/111543 > > gcc/ChangeLog: > > * match.pd (`(X & ~Y) & Y`, `(X | ~Y) | Y`): New patterns. > > gcc/testsuite/ChangeLog: > > * gcc.dg/tree-ssa/bitops-4.c: New test. > --- > gcc/match.pd | 17 +++++++++++++++++ > gcc/testsuite/gcc.dg/tree-ssa/bitops-4.c | 18 ++++++++++++++++++ > 2 files changed, 35 insertions(+) > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bitops-4.c > > diff --git a/gcc/match.pd b/gcc/match.pd > index 0aa815f4118..a17778fbaa6 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -1806,6 +1806,23 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > (simplify > (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1)) > (convert @0)) > + > +/* (X & ~Y) & Y -> 0 */ > +(simplify > + (bit_and:c (bit_and @0 @1) @2) > + (with { bool wascmp; } > + (if (bitwise_inverted_equal_p (@0, @2, wascmp) > + || bitwise_inverted_equal_p (@1, @2, wascmp)) > + { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type= ); }))) > +/* (X | ~Y) | Y -> -1 */ > +(simplify > + (bit_ior:c (bit_ior @0 @1) @2) > + (with { bool wascmp; } > + (if ((bitwise_inverted_equal_p (@0, @2, wascmp) > + || bitwise_inverted_equal_p (@1, @2, wascmp)) > + && (!wascmp || element_precision (type) =3D=3D 1)) > + { build_all_ones_cst (TREE_TYPE (@0)); }))) > + > /* (X & Y) & (X & Z) -> (X & Y) & Z > (X | Y) | (X | Z) -> (X | Y) | Z */ > (for op (bit_and bit_ior) > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bitops-4.c b/gcc/testsuite/gcc= .dg/tree-ssa/bitops-4.c > new file mode 100644 > index 00000000000..73c8f39d28f > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/bitops-4.c > @@ -0,0 +1,18 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O1 -fdump-tree-optimized -fdump-tree-ccp1" } */ > +/* PR tree-optimization/111543 */ > + > +void f_or(int a, int b, int *por) > +{ > + int c =3D ~a; > + *por =3D (c | b) | a; > +} > +void f_and(int a, int b, int *pand) > +{ > + int c =3D ~a; > + *pand =3D (c & b) & a; > +} > +/* { dg-final { scan-tree-dump-times "pand_\[0-9\]+.D. =3D 0" 1 "optimiz= ed" } } */ > +/* { dg-final { scan-tree-dump-times "por_\[0-9\]+.D. =3D -1" 1 "optimiz= ed" } } */ > +/* { dg-final { scan-tree-dump-times "pand_\[0-9\]+.D. =3D 0" 1 "ccp1" }= } */ > +/* { dg-final { scan-tree-dump-times "por_\[0-9\]+.D. =3D -1" 1 "ccp1" }= } */ > -- > 2.31.1 >