From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lj1-x229.google.com (mail-lj1-x229.google.com [IPv6:2a00:1450:4864:20::229]) by sourceware.org (Postfix) with ESMTPS id CEAE23858C41 for ; Mon, 24 Jul 2023 07:31:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CEAE23858C41 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-x229.google.com with SMTP id 38308e7fff4ca-2b70bfc8db5so55714921fa.2 for ; Mon, 24 Jul 2023 00:31:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690183863; x=1690788663; 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=IseKkeDRxwPTZvjRE9Emyn2dV0i7K9GRRD4zp0uOPcY=; b=KUz89mY99/HMvdh8s5q4U745ZTDFlQL0vf1D0BTauvidWxiZzme0Dr5H99XzYI61uS vJoQsAMFOH16qiZy4+YCsGgAxlmaJLfz66E+aMMgqVwNsVyn+XWXVPNW1zS9NkmfJF9W wp8Dq5YPdbaHRdVA3bPFJF9OSHAolAYOu4ofyhKcnOJagIJGJ9xAOwy0BLNmZZ8pQukA CItiFfHPazFxag8zIHqL2346Ii4d1ZYEj/rmtIJCKL/MhUohCTeqvPNAjjtPzDHmiomT nBhIV4epmhR6thHlb1/RuLZGVlu5siJuklwttI7Pe31DE8FiRMLYNuI3BRNuTBFlpbe7 PcVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690183863; x=1690788663; 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=IseKkeDRxwPTZvjRE9Emyn2dV0i7K9GRRD4zp0uOPcY=; b=UROJdD/3vdURVwBGOsk9zvaaKZsj2YhrX+HD0YtCfT3L/FNCLJwjdoixtuZgMNt4YN hqiPM8PkrM5ySwlhdC0B1PJBHZiGPB2z1flnuVDEObsJ5zjp1/+HXviKRuSlOPgQjHaf kf/CFpwx+3kTq7a0OM3un2OiU9y2Th1w4+0MCGbv6KNYovxkwcI+pnvMRTdAdbyyocha W0ItApe5AaDLu6LZaCnZXsXbrlDHbia9aGJ4zCPTowbvRLHTeEdkeiJ1XOkEczmHG6jF 9Vdlc6TZanO4soynWaXU/8kqsZjzxs5ebH+Wzt2nwGUwJXKIV9zuASQK5aBlJmTdbRH2 7Y2Q== X-Gm-Message-State: ABy/qLaPHCYKdeqsfqYjhdaAjbFKl6GWYLZxo8OYsx1+3nWdo7BSheEJ 7SRqvWoNqioQINciFOYUyR3GjD+kyxbaw/KSEZBc4bH9 X-Google-Smtp-Source: APBJJlFCdIeaKPrdHhNOfjRCM1vdvAmgjg1djP2v6fk97jg88VR5+AqD3YHFiIpDSVh+S11Pj7GcseXkBZtaVZ2Jjk8= X-Received: by 2002:a2e:968e:0:b0:2b7:b9ca:3eda with SMTP id q14-20020a2e968e000000b002b7b9ca3edamr5780028lji.34.1690183863217; Mon, 24 Jul 2023 00:31:03 -0700 (PDT) MIME-Version: 1.0 References: <20230721181839.119969-1-drross@redhat.com> In-Reply-To: <20230721181839.119969-1-drross@redhat.com> From: Richard Biener Date: Mon, 24 Jul 2023 09:30:51 +0200 Message-ID: Subject: Re: [PATCH] match.pd, v2: Implement missed optimization (~X | Y) ^ X -> ~(X & Y) [PR109986] To: Drew Ross Cc: gcc-patches@gcc.gnu.org, Jakub Jelinek Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-7.4 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 21, 2023 at 8:19=E2=80=AFPM Drew Ross via Gcc-patches wrote: > > Adds a simplification for (~X | Y) ^ X to be folded into ~(X & Y). > Also adds the macro bitwise_equal_p for generic and gimple which > returns true iff EXPR1 and EXPR2 have the same value. This helps > to reduce the number of nop_converts necessary to match the pattern. > Tested successfully on x86_64 and x86 targets. OK. Thanks, Richard. > PR middle-end/109986 > > gcc/ChangeLog: > > * generic-match-head.cc (bitwise_equal_p): New macro. > * gimple-match-head.cc (bitwise_equal_p): New macro. > (gimple_nop_convert): Declare. > (gimple_bitwise_equal_p): Helper for bitwise_equal_p. > * match.pd ((~X | Y) ^ X -> ~(X & Y)): New simplification. > > gcc/testsuite/ChangeLog: > > * gcc.c-torture/execute/pr109986.c: New test. > * gcc.dg/tree-ssa/pr109986.c: New test. > > Co-authored-by: Jakub Jelinek > --- > gcc/generic-match-head.cc | 17 ++ > gcc/gimple-match-head.cc | 36 ++++ > gcc/match.pd | 6 + > .../gcc.c-torture/execute/pr109986.c | 41 ++++ > gcc/testsuite/gcc.dg/tree-ssa/pr109986.c | 177 ++++++++++++++++++ > 5 files changed, 277 insertions(+) > create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr109986.c > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr109986.c > > diff --git a/gcc/generic-match-head.cc b/gcc/generic-match-head.cc > index f011204c5be..b4b5bc88f4b 100644 > --- a/gcc/generic-match-head.cc > +++ b/gcc/generic-match-head.cc > @@ -102,3 +102,20 @@ optimize_successive_divisions_p (tree, tree) > { > return false; > } > + > +/* Return true if EXPR1 and EXPR2 have the same value, but not necessari= ly > + same type. The types can differ through nop conversions. */ > + > +static inline bool > +bitwise_equal_p (tree expr1, tree expr2) > +{ > + STRIP_NOPS (expr1); > + STRIP_NOPS (expr2); > + if (expr1 =3D=3D expr2) > + return true; > + if (!tree_nop_conversion_p (TREE_TYPE (expr1), TREE_TYPE (expr2))) > + return false; > + if (TREE_CODE (expr1) =3D=3D INTEGER_CST && TREE_CODE (expr2) =3D=3D I= NTEGER_CST) > + return wi::to_wide (expr1) =3D=3D wi::to_wide (expr2); > + return operand_equal_p (expr1, expr2, 0); > +} > diff --git a/gcc/gimple-match-head.cc b/gcc/gimple-match-head.cc > index b08cd891a13..f960d6cf0b9 100644 > --- a/gcc/gimple-match-head.cc > +++ b/gcc/gimple-match-head.cc > @@ -224,3 +224,39 @@ optimize_successive_divisions_p (tree divisor, tree = inner_div) > } > return true; > } > + > +/* Return true if EXPR1 and EXPR2 have the same value, but not necessari= ly > + same type. The types can differ through nop conversions. */ > +#define bitwise_equal_p(expr1, expr2) gimple_bitwise_equal_p (expr1, exp= r2, valueize) > + > +bool gimple_nop_convert (tree, tree *, tree (*)(tree)); > + > +/* Helper function for bitwise_equal_p macro. */ > + > +static inline bool > +gimple_bitwise_equal_p (tree expr1, tree expr2, tree (*valueize) (tree)) > +{ > + if (expr1 =3D=3D expr2) > + return true; > + if (!tree_nop_conversion_p (TREE_TYPE (expr1), TREE_TYPE (expr2))) > + return false; > + if (TREE_CODE (expr1) =3D=3D INTEGER_CST && TREE_CODE (expr2) =3D=3D I= NTEGER_CST) > + return wi::to_wide (expr1) =3D=3D wi::to_wide (expr2); > + if (operand_equal_p (expr1, expr2, 0)) > + return true; > + tree expr3, expr4; > + if (!gimple_nop_convert (expr1, &expr3, valueize)) > + expr3 =3D expr1; > + if (!gimple_nop_convert (expr2, &expr4, valueize)) > + expr4 =3D expr2; > + if (expr1 !=3D expr3) > + { > + if (operand_equal_p (expr3, expr2, 0)) > + return true; > + if (expr2 !=3D expr4 && operand_equal_p (expr3, expr4, 0)) > + return true; > + } > + if (expr2 !=3D expr4 && operand_equal_p (expr1, expr4, 0)) > + return true; > + return false; > +} > diff --git a/gcc/match.pd b/gcc/match.pd > index a17d6838c14..367e4fc5517 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -1627,6 +1627,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) > (convert (bit_and @1 (bit_not @0))))) > > +/* (~X | Y) ^ X -> ~(X & Y). */ > +(simplify > + (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) = @2) > + (if (bitwise_equal_p (@0, @2)) > + (convert (bit_not (bit_and @0 (convert @1)))))) > + > /* Convert ~X ^ ~Y to X ^ Y. */ > (simplify > (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1))) > diff --git a/gcc/testsuite/gcc.c-torture/execute/pr109986.c b/gcc/testsui= te/gcc.c-torture/execute/pr109986.c > new file mode 100644 > index 00000000000..00ee9888539 > --- /dev/null > +++ b/gcc/testsuite/gcc.c-torture/execute/pr109986.c > @@ -0,0 +1,41 @@ > +/* PR middle-end/109986 */ > + > +#include "../../gcc.dg/tree-ssa/pr109986.c" > + > +int > +main () > +{ > + if (t1 (29789, 29477) !=3D -28678) __builtin_abort (); > + if (t2 (20196, -18743) !=3D 4294965567) __builtin_abort (); > + if (t3 (127, 99) !=3D -100) __builtin_abort (); > + if (t4 (100, 53) !=3D 219) __builtin_abort (); > + if (t5 (20100, 1283) !=3D -1025) __builtin_abort (); > + if (t6 (20100, 10283) !=3D 63487) __builtin_abort (); > + if (t7 (2136614690L, 1136698390L) !=3D -1128276995L) __builtin_abort (= ); > + if (t8 (1136698390L, 2136614690L) !=3D -1128276995UL) __builtin_abort = (); > + if (t9 (9176690219839792930LL, 3176690219839721234LL) !=3D -3175044472= 123688707LL) > + __builtin_abort (); > + if (t10 (9176690219839792930LL, 3176690219839721234LL) !=3D 1527169960= 1585862909ULL) > + __builtin_abort (); > + if (t11 (29789, 29477) !=3D -28678) __builtin_abort (); > + if (t12 (20196, -18743) !=3D 4294965567) __builtin_abort (); > + if (t13 (127, 99) !=3D -100) __builtin_abort (); > + if (t14 (100, 53) !=3D 219) __builtin_abort (); > + if (t15 (20100, 1283) !=3D -1025) __builtin_abort (); > + if (t16 (20100, 10283) !=3D 63487) __builtin_abort (); > + if (t17 (2136614690, 1136698390) !=3D -1128276995) __builtin_abort (); > + if (t18 (1136698390L, 2136614690L) !=3D -1128276995UL) __builtin_abort= (); > + if (t19 (9176690219839792930LL, 3176690219839721234LL) !=3D -317504447= 2123688707LL) > + __builtin_abort (); > + if (t20 (9176690219839792930LL, 3176690219839721234LL) !=3D 1527169960= 1585862909ULL) > + __builtin_abort (); > + v4si a1 =3D {1, 2, 3, 4}; > + v4si a2 =3D {6, 7, 8, 9}; > + v4si r1 =3D {-1, -3, -1, -1}; > + v4si b1 =3D t21 (a1, a2); > + v4si b2 =3D t22 (a1, a2); > + if (__builtin_memcmp (&b1, &r1, sizeof (b1) !=3D 0)) __builtin_abort= (); > + if (__builtin_memcmp (&b2, &r1, sizeof (b2) !=3D 0)) __builtin_abort= (); > + return 0; > +} > + > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr109986.c b/gcc/testsuite/gcc= .dg/tree-ssa/pr109986.c > new file mode 100644 > index 00000000000..45f099b5656 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr109986.c > @@ -0,0 +1,177 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-dse1 -Wno-psabi" } */ > + > +typedef int v4si __attribute__((vector_size(16))); > + > +/* Generic */ > +__attribute__((noipa)) int > +t1 (int a, int b) > +{ > + return (~a | b) ^ a; > +} > + > +__attribute__((noipa)) unsigned int > +t2 (int a, int b) > +{ > + return a ^ (~a | (unsigned int) b); > +} > + > +__attribute__((noipa)) char > +t3 (char a, char b) > +{ > + return (b | ~a) ^ a; > +} > + > +__attribute__((noipa)) unsigned char > +t4 (char a, char b) > +{ > + return ((unsigned char) a) ^ (b | ~a); > +} > + > +__attribute__((noipa)) short > +t5 (short a, short b) > +{ > + return a ^ (b | ~a); > +} > + > +__attribute__((noipa)) unsigned short > +t6 (short a, short b) > +{ > + return ((unsigned short) a) ^ (b | ~a); > +} > + > +__attribute__((noipa)) long > +t7 (long a, long b) > +{ > + return a ^ (b | ~a); > +} > + > +__attribute__((noipa)) unsigned long > +t8 (long a, long b) > +{ > + return ((unsigned long) a) ^ (b | ~a); > +} > + > +__attribute__((noipa)) long long > +t9 (long long a, long long b) > +{ > + return a ^ (b | ~a); > +} > + > +__attribute__((noipa)) unsigned long long > +t10 (long long a, long long b) > +{ > + return ((unsigned long long) a) ^ (b | ~a); > +} > + > +__attribute__((noipa)) v4si > +t21 (v4si a, v4si b) > +{ > + return a ^ (b | ~a); > +} > + > +/* Gimple */ > +__attribute__((noipa)) int > +t11 (int a, int b) > +{ > + int t1 =3D ~a; > + int t2 =3D t1 | b; > + int t3 =3D t2 ^ a; > + return t3; > +} > + > +__attribute__((noipa)) unsigned int > +t12 (int a, unsigned int b) > +{ > + int t1 =3D ~a; > + unsigned int t2 =3D t1 | b; > + unsigned int t3 =3D a ^ t2; > + return t3; > +} > + > +__attribute__((noipa)) char > +t13 (char a, char b) > +{ > + char t1 =3D ~a; > + char t2 =3D b | t1; > + char t3 =3D t2 ^ a; > + return t3; > +} > + > +__attribute__((noipa)) unsigned char > +t14 (unsigned char a, char b) > +{ > + unsigned char t1 =3D ~a; > + char t2 =3D b | t1; > + unsigned char t3 =3D a ^ t2; > + return t3; > +} > + > +__attribute__((noipa)) short > +t15 (short a, short b) > +{ > + short t1 =3D ~a; > + short t2 =3D t1 | b; > + short t3 =3D t2 ^ a; > + return t3; > +} > + > +__attribute__((noipa)) unsigned short > +t16 (unsigned short a, short b) > +{ > + short t1 =3D ~a; > + short t2 =3D t1 | b; > + unsigned short t3 =3D t2 ^ a; > + return t3; > +} > + > +__attribute__((noipa)) long > +t17 (long a, long b) > +{ > + long t1 =3D ~a; > + long t2 =3D t1 | b; > + long t3 =3D t2 ^ a; > + return t3; > +} > + > +__attribute__((noipa)) unsigned long > +t18 (long a, unsigned long b) > +{ > + long t1 =3D ~a; > + unsigned long t2 =3D t1 | b; > + unsigned long t3 =3D t2 ^ a; > + return t3; > +} > + > +__attribute__((noipa)) long long > +t19 (long long a, long long b) > +{ > + long long t1 =3D ~a; > + long long t2 =3D t1 | b; > + long long t3 =3D t2 ^ a; > + return t3; > +} > + > +__attribute__((noipa)) unsigned long long > +t20 (long long a, long long b) > +{ > + long long t1 =3D ~a; > + long long t2 =3D t1 | b; > + unsigned long long t3 =3D a ^ t2; > + return t3; > +} > + > +__attribute__((noipa)) v4si > +t22 (v4si a, v4si b) > +{ > + v4si t1 =3D ~a; > + v4si t2 =3D t1 | b; > + v4si t3 =3D a ^ t2; > + return t3; > +} > + > +/* { dg-final { scan-tree-dump-not " \\\| " "dse1" } } */ > +/* { dg-final { scan-tree-dump-not " \\\^ " "dse1" } } */ > +/* { dg-final { scan-tree-dump-times " ~" 22 "dse1" } } */ > +/* { dg-final { scan-tree-dump-times " & " 22 "dse1" } } */ > + > -- > 2.39.3 >