From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by sourceware.org (Postfix) with ESMTPS id 01A2A389364E for ; Fri, 21 Jun 2024 03:10:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 01A2A389364E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 01A2A389364E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1031 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718939441; cv=none; b=RTvwoRs8DJKDC6/fcAt/OGaXBg00/iwuv5vmvqs5NUPvthUiwYZZVLgz4Uo22gHkWQI/FPkx9wZ9mmlnlk/21N52fhVEBlTYwhN/mWDD3tDPITP9Zc3GFok7KLegtwAp+AmG9phoOnGvEAYOyIuodRwq/sc7dyZJ0kTp5NTE794= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718939441; c=relaxed/simple; bh=yEH7aXXleqLo+FBeYyrtSSO7Lg11wuWu5Z9539vIcLk=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=Qi6pe73qEGBKcGd+WQjzHUYlOXEW6FFCIB+dlAorMLXYkEfYB0A6sI5VgL28ZiPc2K6cediHNmblqHWVnQn6mL/hbebK4aC0AV0MYqigNMPiJ+FvqynD2CZYbd60yIVJqwRTF76KNjgVixBXu1pxlnzbgU7YOFUKiebLwUqzbHg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pj1-x1031.google.com with SMTP id 98e67ed59e1d1-2c2c6277ef3so1278763a91.3 for ; Thu, 20 Jun 2024 20:10:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718939438; x=1719544238; 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=zAcoYzG+pgiKZ/9nuBU9iEhpl7doSU3+u4FxXmKQMX8=; b=CdcqqH0SR7GwX+RYm2sXrpbCwVygWvuJt6IDuMbX8AD7AdMZbt0QcN1SndiQVJ8ON8 grVJbIbIx0rvhsZi1L76iduAUCAtE88vVE46izrDV2Ky7BvsbLiZUrzs904sBkpZ7WnB +a5yFXE5WRzLfDNYiJZmwoPup+RilS7adXcnNg+2wW6mzk6AhNLkaEipSfAbhwZsnU58 PapKs3ylGffEsZTYmfhAvOqNrCi8/tY4rNH33tnYoBFWZeaQiadgKeqtnORY1K0ilg+d DPYpHraK4J3UaBy+TkhqCnCAbXyHWJKUxfhoyn8FB5WPmp0vAcaEMX4P7xPcNG4CegJL uPLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718939438; x=1719544238; 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=zAcoYzG+pgiKZ/9nuBU9iEhpl7doSU3+u4FxXmKQMX8=; b=lze/DRm3AcTSAYTOfGjydwQ3sGjSKEv6YBOdolFfdhTM3Wsm16xyUKkVxRl+nhXT6J Bj7GxSG1YezEMl9yu2trIFfCC1hP8AKxu/k9xiLNuCqYMrand4sLk6BuWqKWoeNCs1jE mltlbPhQwabfTCGYZOW3Uh9ypoIJZ/FYwcV0qPfJVvCoBM5zh1svY/OdY4qBZcP1jX/i 39LcGUMa4GEELgNsAu1I/LVWJyKlOS57rNPeJZyFBsNrNqvB1vYVeJueWaQCl4eICwgj eBWyZW5sAg8ClnqzXvpw3bSnVoVr0Fnpki74AUdfzPavUV8nCHm8dctppyhcDZ9rpWGr BouA== X-Gm-Message-State: AOJu0YzBBhxsz8It6INsPgGn5FhV6oo/zPodynn2Its/MUhEqJm9QEyP SiziBGZI43cYdzVisxN8Fit66cGNXmzXTMyrfROlOV5vGLkAlAWBYpzglBCvYZzaZtia2JlZKu+ ot5iEP66/QffRok6SEQ0ELjCa1hg= X-Google-Smtp-Source: AGHT+IHdZg0qyNmKu19Wi/vMOBxRya+fHv8YeWK4wjStBbcmBmUNKKLFCzdkdWdTZ7ZQZw0A3xtxxUem1SbkioNGqVo= X-Received: by 2002:a17:90a:1f8f:b0:2c4:f5ad:c34b with SMTP id 98e67ed59e1d1-2c7b5cd23a8mr7078256a91.28.1718939437636; Thu, 20 Jun 2024 20:10:37 -0700 (PDT) MIME-Version: 1.0 References: <20240621025543.2470827-1-hongtao.liu@intel.com> In-Reply-To: <20240621025543.2470827-1-hongtao.liu@intel.com> From: Andrew Pinski Date: Thu, 20 Jun 2024 20:10:25 -0700 Message-ID: Subject: Re: [PATCH] [match.pd] Optimize a < 0 ? -1 : 0 to (signed)a >> 31. To: liuhongt Cc: gcc-patches@gcc.gnu.org, crazylht@gmail.com, hjl.tools@gmail.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,KAM_SHORT,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 Thu, Jun 20, 2024 at 7:56=E2=80=AFPM liuhongt wr= ote: > > Try to optimize x < 0 ? -1 : 0 into (signed) x >> 31 > and x < 0 ? 1 : 0 into (unsigned) x >> 31. > > Move the optimization did in ix86_expand_int_vcond to match.pd > > Bootstrapped and regtested on x86_64-pc-linux-gnu{-m32,}, aarch64-linux-g= nu. > Ok for trunk? > > gcc/ChangeLog: > > PR target/114189 > * match.pd: Simplify a < 0 ? -1 : 0 to (signed) >> 31 and a < > 0 ? 1 : 0 to (unsigned) a >> 31 for vector integer type. > > gcc/testsuite/ChangeLog: > > * gcc.target/i386/avx2-pr115517.c: New test. > * gcc.target/i386/avx512-pr115517.c: New test. > * g++.target/i386/avx2-pr115517.C: New test. > * g++.target/i386/avx512-pr115517.C: New test. > * g++.dg/tree-ssa/pr88152-1.C: Adjust testcase. > --- > gcc/match.pd | 28 ++++++++ > gcc/testsuite/g++.dg/tree-ssa/pr88152-1.C | 2 +- > gcc/testsuite/g++.target/i386/avx2-pr115517.C | 60 ++++++++++++++++ > .../g++.target/i386/avx512-pr115517.C | 70 +++++++++++++++++++ > gcc/testsuite/gcc.target/i386/avx2-pr115517.c | 33 +++++++++ > .../gcc.target/i386/avx512-pr115517.c | 70 +++++++++++++++++++ > 6 files changed, 262 insertions(+), 1 deletion(-) > create mode 100644 gcc/testsuite/g++.target/i386/avx2-pr115517.C > create mode 100644 gcc/testsuite/g++.target/i386/avx512-pr115517.C > create mode 100644 gcc/testsuite/gcc.target/i386/avx2-pr115517.c > create mode 100644 gcc/testsuite/gcc.target/i386/avx512-pr115517.c > > diff --git a/gcc/match.pd b/gcc/match.pd > index 3d0689c9312..41dd90493e7 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -5927,6 +5927,34 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > (if (VECTOR_INTEGER_TYPE_P (type) > && target_supports_op_p (type, MINMAX, optab_vector)) > (minmax @0 @1)))) > + > +/* Try to optimize x < 0 ? -1 : 0 into (signed) x >> 31 > + and x < 0 ? 1 : 0 into (unsigned) x >> 31. */ > +(simplify > + (vec_cond (lt @0 integer_zerop) integer_all_onesp integer_zerop) > + (if (VECTOR_INTEGER_TYPE_P (type) > + && !TYPE_UNSIGNED (type) > + && target_supports_op_p (type, RSHIFT_EXPR, optab_scalar)) I think the check for TYPE_UNSIGNED should be of TREE_TYPE (@0) rather than type here. Or maybe you need `types_match (type, TREE_TYPE (@0))` too. > + (with > + { > + unsigned int prec =3D element_precision (type); > + } > + (rshift @0 { build_int_cst (integer_type_node, prec - 1);})))) You might be missing some if you don't change to use TREE_TYPE (@0). With the TREE_TYPE change you need to add a VCE to the type though. E.g. ``` typedef short v8hi __attribute__((vector_size(16))); typedef unsigned short uv8hi __attribute__((vector_size(16))); uv8hi foo (v8hi a) { return a < 0 ; } ``` > + > +(simplify > + (vec_cond (lt @0 integer_zerop) integer_onep integer_zerop) > + (if (VECTOR_INTEGER_TYPE_P (type) > + && !TYPE_UNSIGNED (type) > + && target_supports_op_p (unsigned_type_for (type), > + RSHIFT_EXPR, optab_scalar)) > + (with > + { > + unsigned int prec =3D element_precision (type); > + tree utype =3D unsigned_type_for (type); > + } > + (view_convert:type > + (rshift (view_convert:utype @0) > + { build_int_cst (integer_type_node, prec - 1);}))))) I suspect this pattern has a similar issue too. Thanks, Andrew Pinski > #endif > > (for cnd (cond vec_cond) > diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr88152-1.C b/gcc/testsuite/g+= +.dg/tree-ssa/pr88152-1.C > index 423ec897c1d..21299b886f0 100644 > --- a/gcc/testsuite/g++.dg/tree-ssa/pr88152-1.C > +++ b/gcc/testsuite/g++.dg/tree-ssa/pr88152-1.C > @@ -1,7 +1,7 @@ > // PR target/88152 > // { dg-do compile } > // { dg-options "-O2 -std=3Dc++14 -fdump-tree-forwprop1" } > -// { dg-final { scan-tree-dump-times " (?:<|>=3D) \{ 0\[, ]" 120 "forwpr= op1" } } > +// { dg-final { scan-tree-dump-times " (?:(?:<|>=3D) \{ 0\[, \]|>> (?:7|= 15|31|63))" 120 "forwprop1" } } > > template > using V [[gnu::vector_size (sizeof (T) * N)]] =3D T; > diff --git a/gcc/testsuite/g++.target/i386/avx2-pr115517.C b/gcc/testsuit= e/g++.target/i386/avx2-pr115517.C > new file mode 100644 > index 00000000000..ec000c57542 > --- /dev/null > +++ b/gcc/testsuite/g++.target/i386/avx2-pr115517.C > @@ -0,0 +1,60 @@ > +/* { dg-do compile } */ > +/* { dg-options "-mavx2 -O2" } */ > +/* { dg-final { scan-assembler-times "vpsrlq" 2 } } */ > +/* { dg-final { scan-assembler-times "vpsrld" 2 } } */ > +/* { dg-final { scan-assembler-times "vpsrlw" 2 } } */ > + > +typedef short v8hi __attribute__((vector_size(16))); > +typedef short v16hi __attribute__((vector_size(32))); > +typedef int v4si __attribute__((vector_size(16))); > +typedef int v8si __attribute__((vector_size(32))); > +typedef long long v2di __attribute__((vector_size(16))); > +typedef long long v4di __attribute__((vector_size(32))); > + > +v8hi > +foo (v8hi a) > +{ > + v8hi const1_op =3D __extension__(v8hi){1,1,1,1,1,1,1,1}; > + v8hi const0_op =3D __extension__(v8hi){0,0,0,0,0,0,0,0}; > + return a < const0_op ? const1_op : const0_op; > +} > + > +v16hi > +foo2 (v16hi a) > +{ > + v16hi const1_op =3D __extension__(v16hi){1,1,1,1,1,1,1,1,1,1,1,1,1,1,1= ,1}; > + v16hi const0_op =3D __extension__(v16hi){0,0,0,0,0,0,0,0,0,0,0,0,0,0,0= ,0}; > + return a < const0_op ? const1_op : const0_op; > +} > + > +v4si > +foo3 (v4si a) > +{ > + v4si const1_op =3D __extension__(v4si){1,1,1,1}; > + v4si const0_op =3D __extension__(v4si){0,0,0,0}; > + return a < const0_op ? const1_op : const0_op; > +} > + > +v8si > +foo4 (v8si a) > +{ > + v8si const1_op =3D __extension__(v8si){1,1,1,1,1,1,1,1}; > + v8si const0_op =3D __extension__(v8si){0,0,0,0,0,0,0,0}; > + return a < const0_op ? const1_op : const0_op; > +} > + > +v2di > +foo3 (v2di a) > +{ > + v2di const1_op =3D __extension__(v2di){1,1}; > + v2di const0_op =3D __extension__(v2di){0,0}; > + return a < const0_op ? const1_op : const0_op; > +} > + > +v4di > +foo4 (v4di a) > +{ > + v4di const1_op =3D __extension__(v4di){1,1,1,1}; > + v4di const0_op =3D __extension__(v4di){0,0,0,0}; > + return a < const0_op ? const1_op : const0_op; > +} > diff --git a/gcc/testsuite/g++.target/i386/avx512-pr115517.C b/gcc/testsu= ite/g++.target/i386/avx512-pr115517.C > new file mode 100644 > index 00000000000..22df41bbdc9 > --- /dev/null > +++ b/gcc/testsuite/g++.target/i386/avx512-pr115517.C > @@ -0,0 +1,70 @@ > +/* { dg-do compile } */ > +/* { dg-options "-mavx512bw -mavx512vl -O2" } */ > +/* { dg-final { scan-assembler-times "vpsrad" 3 } } */ > +/* { dg-final { scan-assembler-times "vpsraw" 3 } } */ > +/* { dg-final { scan-assembler-times "vpsraq" 3 } } */ > + > +typedef short v8hi __attribute__((vector_size(16))); > +typedef short v16hi __attribute__((vector_size(32))); > +typedef short v32hi __attribute__((vector_size(64))); > +typedef int v4si __attribute__((vector_size(16))); > +typedef int v8si __attribute__((vector_size(32))); > +typedef int v16si __attribute__((vector_size(64))); > +typedef long long v2di __attribute__((vector_size(16))); > +typedef long long v4di __attribute__((vector_size(32))); > +typedef long long v8di __attribute__((vector_size(64))); > + > +v8hi > +foo (v8hi a) > +{ > + return a < __extension__(v8hi) { 0, 0, 0, 0, 0, 0, 0, 0}; > +} > + > +v16hi > +foo2 (v16hi a) > +{ > + return a < __extension__(v16hi) { 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0= , 0, 0, 0}; > +} > + > +v32hi > +foo3 (v32hi a) > +{ > + return a < __extension__(v32hi) { 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0= , 0, 0, 0, > + 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0}; > +} > + > +v4si > +foo4 (v4si a) > +{ > + return a < __extension__(v4si) { 0, 0, 0, 0}; > +} > + > +v8si > +foo5 (v8si a) > +{ > + return a < __extension__(v8si) { 0, 0, 0, 0, 0, 0, 0, 0}; > +} > + > +v16si > +foo6 (v16si a) > +{ > + return a < __extension__(v16si) { 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0= , 0, 0, 0}; > +} > + > +v2di > +foo7 (v2di a) > +{ > + return a < __extension__(v2di) { 0, 0}; > +} > + > +v4di > +foo8 (v4di a) > +{ > + return a < __extension__(v4di) { 0, 0, 0, 0}; > +} > + > +v8di > +foo9 (v8di a) > +{ > + return a < __extension__(v8di) { 0, 0, 0, 0, 0, 0, 0, 0}; > +} > diff --git a/gcc/testsuite/gcc.target/i386/avx2-pr115517.c b/gcc/testsuit= e/gcc.target/i386/avx2-pr115517.c > new file mode 100644 > index 00000000000..5b2620b0dc1 > --- /dev/null > +++ b/gcc/testsuite/gcc.target/i386/avx2-pr115517.c > @@ -0,0 +1,33 @@ > +/* { dg-do compile } */ > +/* { dg-options "-mavx2 -O2" } */ > +/* { dg-final { scan-assembler-times "vpsrad" 2 } } */ > +/* { dg-final { scan-assembler-times "vpsraw" 2 } } */ > + > +typedef short v8hi __attribute__((vector_size(16))); > +typedef short v16hi __attribute__((vector_size(32))); > +typedef int v4si __attribute__((vector_size(16))); > +typedef int v8si __attribute__((vector_size(32))); > + > +v8hi > +foo (v8hi a) > +{ > + return a < __extension__(v8hi) { 0, 0, 0, 0, 0, 0, 0, 0}; > +} > + > +v16hi > +foo2 (v16hi a) > +{ > + return a < __extension__(v16hi) { 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0= , 0, 0, 0}; > +} > + > +v4si > +foo3 (v4si a) > +{ > + return a < __extension__(v4si) { 0, 0, 0, 0}; > +} > + > +v8si > +foo4 (v8si a) > +{ > + return a < __extension__(v8si) { 0, 0, 0, 0, 0, 0, 0, 0}; > +} > diff --git a/gcc/testsuite/gcc.target/i386/avx512-pr115517.c b/gcc/testsu= ite/gcc.target/i386/avx512-pr115517.c > new file mode 100644 > index 00000000000..22df41bbdc9 > --- /dev/null > +++ b/gcc/testsuite/gcc.target/i386/avx512-pr115517.c > @@ -0,0 +1,70 @@ > +/* { dg-do compile } */ > +/* { dg-options "-mavx512bw -mavx512vl -O2" } */ > +/* { dg-final { scan-assembler-times "vpsrad" 3 } } */ > +/* { dg-final { scan-assembler-times "vpsraw" 3 } } */ > +/* { dg-final { scan-assembler-times "vpsraq" 3 } } */ > + > +typedef short v8hi __attribute__((vector_size(16))); > +typedef short v16hi __attribute__((vector_size(32))); > +typedef short v32hi __attribute__((vector_size(64))); > +typedef int v4si __attribute__((vector_size(16))); > +typedef int v8si __attribute__((vector_size(32))); > +typedef int v16si __attribute__((vector_size(64))); > +typedef long long v2di __attribute__((vector_size(16))); > +typedef long long v4di __attribute__((vector_size(32))); > +typedef long long v8di __attribute__((vector_size(64))); > + > +v8hi > +foo (v8hi a) > +{ > + return a < __extension__(v8hi) { 0, 0, 0, 0, 0, 0, 0, 0}; > +} > + > +v16hi > +foo2 (v16hi a) > +{ > + return a < __extension__(v16hi) { 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0= , 0, 0, 0}; > +} > + > +v32hi > +foo3 (v32hi a) > +{ > + return a < __extension__(v32hi) { 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0= , 0, 0, 0, > + 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0}; > +} > + > +v4si > +foo4 (v4si a) > +{ > + return a < __extension__(v4si) { 0, 0, 0, 0}; > +} > + > +v8si > +foo5 (v8si a) > +{ > + return a < __extension__(v8si) { 0, 0, 0, 0, 0, 0, 0, 0}; > +} > + > +v16si > +foo6 (v16si a) > +{ > + return a < __extension__(v16si) { 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0= , 0, 0, 0}; > +} > + > +v2di > +foo7 (v2di a) > +{ > + return a < __extension__(v2di) { 0, 0}; > +} > + > +v4di > +foo8 (v4di a) > +{ > + return a < __extension__(v4di) { 0, 0, 0, 0}; > +} > + > +v8di > +foo9 (v8di a) > +{ > + return a < __extension__(v8di) { 0, 0, 0, 0, 0, 0, 0, 0}; > +} > -- > 2.31.1 >