From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by sourceware.org (Postfix) with ESMTPS id 3DD8C3858D3C for ; Tue, 12 Sep 2023 10:37:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3DD8C3858D3C 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-x62d.google.com with SMTP id d9443c01a7336-1c09673b006so36677845ad.1 for ; Tue, 12 Sep 2023 03:37:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694515046; x=1695119846; 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=Kom3yBsyM+/m/z1j1EPJOes3FdL4sP+rIDMVr3be/bA=; b=G2bUll/HEEyM1htYq/5WSzj085ob/QoDsTn0eT6/f989JR4voEIwMoYahL0HODOMqE Ipi9GEucJdbDOOomIsuxk7oVXpa6iWgHT1WtJ4hf10c6ApKgQI+af59JtM3z/gah24I/ scvKVu8ZKHPzV9UnqYRF7P7s+XNT6p3bNkyWq+9boLnipVfyxGAHCnnf07/p6igxSbyv vQ68mS7ztf7MXtqwpC2GDEOY8HpZDWtsXai5ApYsNCZQaPe0SEhXZSAKHHiAKfAhCAyU 1Yx+/pKZ+t4SB/lWayOV7awJEtjYYTjgLrUd2O6uK5klkbJACT8ZLHgTiUIZFqUufzNe 8m2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694515046; x=1695119846; 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=Kom3yBsyM+/m/z1j1EPJOes3FdL4sP+rIDMVr3be/bA=; b=NhO6899s9AXbb4KKOL1NaWCj12R/zDCxf6/pm2oX35UACoOX+MvqooIqv0O10z6KNs d7TjHLxVSkmCWoVu9Se/OdMWGan8mLXMu7f6twb1Ra/zZxA47bP5UspiUte5CqCXg2Zl rM14+6I3BneVXB6bosRT7SuXcunFU/yw/PazRNj0dOHQ58sj6vfaELJjFqCrBM0+0c/U iwX1WONR2JNDJPjjFRAPsqsfT3NH+IMcZn1Bh2CrAYDQC1PytBBSAo0MCtZCRXtIFUvP w5Aof6EnrXXSbLoQKwuvaTmRuv050RJHAq6hTbrJfDmpXiMXouZNbWq+cCaACdCLwh/B RBgQ== X-Gm-Message-State: AOJu0YwFZ9fVRAxIBDJ19I/LtWtd8PTqTTPFOpSokTAfd0HjCcWSLBDS UnCjPz3k1nel8hqN2YhUgTxGajvuzyU3JZXfpTk= X-Google-Smtp-Source: AGHT+IHo6RNmoVlgKD1TMQv4I66PpkO5eHyx3lgSIzFcXUUh4vfXltSiVbdVMbvo3wgnIsI2ssJ02ff0qa0oxd9Rf4I= X-Received: by 2002:a17:90b:4b09:b0:274:111c:c14d with SMTP id lx9-20020a17090b4b0900b00274111cc14dmr4416191pjb.13.1694515045811; Tue, 12 Sep 2023 03:37:25 -0700 (PDT) MIME-Version: 1.0 References: <20230912042152.1412606-1-apinski@marvell.com> In-Reply-To: <20230912042152.1412606-1-apinski@marvell.com> From: Richard Biener Date: Tue, 12 Sep 2023 12:35:19 +0200 Message-ID: Subject: Re: [PATCH] MATCH: Simplify (a CMP1 b) ^ (a CMP2 b) 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.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 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 Tue, Sep 12, 2023 at 6:22=E2=80=AFAM Andrew Pinski via Gcc-patches wrote: > > This adds the missing optimizations here. > Note we don't need to match where CMP1 and CMP2 are complements of each > other as that is already handled elsewhere. > > I added a new executable testcase to make sure we optimize it correctly > as I had originally messed up one of the entries for the resulting > comparison to make sure they were 100% correct. > > OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. OK. > PR tree-optimization/107881 > > gcc/ChangeLog: > > * match.pd (`(a CMP1 b) ^ (a CMP2 b)`): New pattern. > (`(a CMP1 b) =3D=3D (a CMP2 b)`): New pattern. > > gcc/testsuite/ChangeLog: > > * gcc.c-torture/execute/pr107881-1.c: New test. > * gcc.dg/tree-ssa/cmpeq-4.c: New test. > * gcc.dg/tree-ssa/cmpxor-1.c: New test. > --- > gcc/match.pd | 20 +++ > .../gcc.c-torture/execute/pr107881-1.c | 115 ++++++++++++++++++ > gcc/testsuite/gcc.dg/tree-ssa/cmpeq-4.c | 51 ++++++++ > gcc/testsuite/gcc.dg/tree-ssa/cmpxor-1.c | 51 ++++++++ > 4 files changed, 237 insertions(+) > create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr107881-1.c > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cmpeq-4.c > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cmpxor-1.c > > diff --git a/gcc/match.pd b/gcc/match.pd > index e96e385c6fa..39c7ea1088f 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -3154,6 +3154,26 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > { constant_boolean_node (true, type); }) > )))))) > > +/* Optimize (a CMP b) ^ (a CMP b) */ > +/* Optimize (a CMP b) !=3D (a CMP b) */ > +(for op (bit_xor ne) > + (for cmp1 (lt lt lt le le le) > + cmp2 (gt eq ne ge eq ne) > + rcmp (ne le gt ne lt ge) > + (simplify > + (op:c (cmp1:c @0 @1) (cmp2:c @0 @1)) > + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@= 0))) > + (rcmp @0 @1))))) > + > +/* Optimize (a CMP b) =3D=3D (a CMP b) */ > +(for cmp1 (lt lt lt le le le) > + cmp2 (gt eq ne ge eq ne) > + rcmp (eq gt le eq ge lt) > + (simplify > + (eq:c (cmp1:c @0 @1) (cmp2:c @0 @1)) > + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0= ))) > + (rcmp @0 @1)))) > + > /* We can't reassociate at all for saturating types. */ > (if (!TYPE_SATURATING (type)) > > diff --git a/gcc/testsuite/gcc.c-torture/execute/pr107881-1.c b/gcc/tests= uite/gcc.c-torture/execute/pr107881-1.c > new file mode 100644 > index 00000000000..063ec4c2797 > --- /dev/null > +++ b/gcc/testsuite/gcc.c-torture/execute/pr107881-1.c > @@ -0,0 +1,115 @@ > +#define func(vol, op1, op2, op3) \ > +_Bool op1##_##op2##_##op3##_##vol (int a, int b) \ > +{ \ > + vol _Bool x =3D op_##op1(a, b); \ > + vol _Bool y =3D op_##op2(a, b); \ > + return op_##op3(x, y); \ > +} > + > +#define op_lt(a, b) ((a) < (b)) > +#define op_le(a, b) ((a) <=3D (b)) > +#define op_eq(a, b) ((a) =3D=3D (b)) > +#define op_ne(a, b) ((a) !=3D (b)) > +#define op_gt(a, b) ((a) > (b)) > +#define op_ge(a, b) ((a) >=3D (b)) > +#define op_xor(a, b) ((a) ^ (b)) > + > + > +#define funcs(a) \ > + a(lt,lt,ne) \ > + a(lt,lt,eq) \ > + a(lt,lt,xor) \ > + a(lt,le,ne) \ > + a(lt,le,eq) \ > + a(lt,le,xor) \ > + a(lt,gt,ne) \ > + a(lt,gt,eq) \ > + a(lt,gt,xor) \ > + a(lt,ge,ne) \ > + a(lt,ge,eq) \ > + a(lt,ge,xor) \ > + a(lt,eq,ne) \ > + a(lt,eq,eq) \ > + a(lt,eq,xor) \ > + a(lt,ne,ne) \ > + a(lt,ne,eq) \ > + a(lt,ne,xor) \ > + \ > + a(le,lt,ne) \ > + a(le,lt,eq) \ > + a(le,lt,xor) \ > + a(le,le,ne) \ > + a(le,le,eq) \ > + a(le,le,xor) \ > + a(le,gt,ne) \ > + a(le,gt,eq) \ > + a(le,gt,xor) \ > + a(le,ge,ne) \ > + a(le,ge,eq) \ > + a(le,ge,xor) \ > + a(le,eq,ne) \ > + a(le,eq,eq) \ > + a(le,eq,xor) \ > + a(le,ne,ne) \ > + a(le,ne,eq) \ > + a(le,ne,xor) \ > + \ > + a(gt,lt,ne) \ > + a(gt,lt,eq) \ > + a(gt,lt,xor) \ > + a(gt,le,ne) \ > + a(gt,le,eq) \ > + a(gt,le,xor) \ > + a(gt,gt,ne) \ > + a(gt,gt,eq) \ > + a(gt,gt,xor) \ > + a(gt,ge,ne) \ > + a(gt,ge,eq) \ > + a(gt,ge,xor) \ > + a(gt,eq,ne) \ > + a(gt,eq,eq) \ > + a(gt,eq,xor) \ > + a(gt,ne,ne) \ > + a(gt,ne,eq) \ > + a(gt,ne,xor) \ > + \ > + a(ge,lt,ne) \ > + a(ge,lt,eq) \ > + a(ge,lt,xor) \ > + a(ge,le,ne) \ > + a(ge,le,eq) \ > + a(ge,le,xor) \ > + a(ge,gt,ne) \ > + a(ge,gt,eq) \ > + a(ge,gt,xor) \ > + a(ge,ge,ne) \ > + a(ge,ge,eq) \ > + a(ge,ge,xor) \ > + a(ge,eq,ne) \ > + a(ge,eq,eq) \ > + a(ge,eq,xor) \ > + a(ge,ne,ne) \ > + a(ge,ne,eq) \ > + a(ge,ne,xor) > + > +#define funcs1(a,b,c) \ > +func(,a,b,c) \ > +func(volatile,a,b,c) > + > +funcs(funcs1) > + > +#define test(op1,op2,op3) \ > +do { \ > + if (op1##_##op2##_##op3##_(x,y) \ > + !=3D op1##_##op2##_##op3##_volatile(x,y)) \ > + __builtin_abort(); \ > +} while(0); > + > +int main() > +{ > + for(int x =3D -10; x < 10; x++) > + for(int y =3D -10; y < 10; y++) > + { > + funcs(test) > + } > +} > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cmpeq-4.c b/gcc/testsuite/gcc.= dg/tree-ssa/cmpeq-4.c > new file mode 100644 > index 00000000000..868d80fdcca > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/cmpeq-4.c > @@ -0,0 +1,51 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-optimized -fdump-tree-original" } */ > +/* PR tree-optimization/107881 */ > + > +_Bool ltgt_eq(int a, int b) > +{ > + _Bool c =3D a < b; > + _Bool d =3D a > b; > + return c =3D=3D d; // a =3D=3D b > +} > +/* { dg-final { scan-tree-dump "a_\[0-9\]+.D. =3D=3D b_\[0-9\]+.D.|b_\[0= -9\]+.D. =3D=3D a_\[0-9\]+.D." "optimized" } } */ > + > +_Bool lteq_eq(int x, int y) > +{ > + _Bool c =3D x < y; > + _Bool d =3D x =3D=3D y; > + return c =3D=3D d; // x > y > +} > +/* { dg-final { scan-tree-dump "x_\[0-9\]+.D. > y_\[0-9\]+.D.|y_\[0-9\]+= .D. < x_\[0-9\]+.D." "optimized" } } */ > + > +_Bool ltne_eq(int z, int w) > +{ > + _Bool c =3D z < w; > + _Bool d =3D z !=3D w; > + return c =3D=3D d; // z <=3D w > +} > +/* { dg-final { scan-tree-dump "z_\[0-9\]+.D. <=3D w_\[0-9\]+.D.|w_\[0-9= \]+.D. >=3D y_\[0-9\]+.D." "optimized" } } */ > + > +_Bool lege_eq(int i, int j) > +{ > + _Bool c =3D i <=3D j; > + _Bool d =3D i >=3D j; > + return c =3D=3D d; // i =3D=3D j > +} > +/* { dg-final { scan-tree-dump "i_\[0-9\]+.D. =3D=3D j_\[0-9\]+.D.|j_\[0= -9\]+.D. =3D=3D i_\[0-9\]+.D." "optimized" } } */ > + > +_Bool leeq_eq(int k, int l) > +{ > + _Bool c =3D k <=3D l; > + _Bool d =3D k =3D=3D l; > + return c =3D=3D d; // k >=3D l > +} > +/* { dg-final { scan-tree-dump "k_\[0-9\]+.D. >=3D l_\[0-9\]+.D.|l_\[0-9= \]+.D. <=3D k_\[0-9\]+.D." "optimized" } } */ > + > +_Bool lene_eq(int m, int n) > +{ > + _Bool c =3D m <=3D n; > + _Bool d =3D m !=3D n; > + return c =3D=3D d; // m < n > +} > +/* { dg-final { scan-tree-dump "m_\[0-9\]+.D. < n_\[0-9\]+.D.|n_\[0-9\]+= .D. > m_\[0-9\]+.D." "optimized" } } */ > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cmpxor-1.c b/gcc/testsuite/gcc= .dg/tree-ssa/cmpxor-1.c > new file mode 100644 > index 00000000000..8de2d9d2244 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/cmpxor-1.c > @@ -0,0 +1,51 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-optimized" } */ > +/* PR tree-optimization/107881 */ > + > +_Bool ltgtxor(int a, int b) > +{ > + _Bool c =3D a < b; > + _Bool d =3D a > b; > + return c ^ d; // a !=3D b > +} > +/* { dg-final { scan-tree-dump "a_\[0-9\]+.D. !=3D b_\[0-9\]+.D.|b_\[0-9= \]+.D. !=3D a_\[0-9\]+.D." "optimized" } } */ > + > +_Bool lteqxor(int x, int y) > +{ > + _Bool c =3D x < y; > + _Bool d =3D x =3D=3D y; > + return c ^ d; // x <=3D y (basically | here) > +} > +/* { dg-final { scan-tree-dump "x_\[0-9\]+.D. <=3D y_\[0-9\]+.D.|y_\[0-9= \]+.D. >=3D x_\[0-9\]+.D." "optimized" } } */ > + > +_Bool ltnexor(int z, int w) > +{ > + _Bool c =3D z < w; > + _Bool d =3D z !=3D w; > + return c ^ d; // z > w > +} > +/* { dg-final { scan-tree-dump "z_\[0-9\]+.D. > w_\[0-9\]+.D.|w_\[0-9\]+= .D. < y_\[0-9\]+.D." "optimized" } } */ > + > +_Bool legexor(int i, int j) > +{ > + _Bool c =3D i <=3D j; > + _Bool d =3D i >=3D j; > + return c ^ d; // i !=3D j > +} > +/* { dg-final { scan-tree-dump "i_\[0-9\]+.D. !=3D j_\[0-9\]+.D.|j_\[0-9= \]+.D. !=3D i_\[0-9\]+.D." "optimized" } } */ > + > +_Bool leeqxor(int k, int l) > +{ > + _Bool c =3D k <=3D l; > + _Bool d =3D k =3D=3D l; > + return c ^ d; // k < l > +} > +/* { dg-final { scan-tree-dump "k_\[0-9\]+.D. < l_\[0-9\]+.D.|l_\[0-9\]+= .D. > k_\[0-9\]+.D." "optimized" } } */ > + > +_Bool lenexor(int m, int n) > +{ > + _Bool c =3D m <=3D n; > + _Bool d =3D m !=3D n; > + return c ^ d; // m >=3D n > +} > +/* { dg-final { scan-tree-dump "m_\[0-9\]+.D. >=3D n_\[0-9\]+.D.|n_\[0-9= \]+.D. <=3D m_\[0-9\]+.D." "optimized" } } */ > -- > 2.31.1 >