public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Uros Bizjak <ubizjak@gmail.com>
To: Roger Sayle <roger@nextmovesoftware.com>
Cc: gcc-patches@gcc.gnu.org
Subject: Re: [x86_64 PATCH] Use PTEST to perform AND in TImode STV of (A & B) != 0.
Date: Tue, 9 Aug 2022 10:43:33 +0200	[thread overview]
Message-ID: <CAFULd4brLgO0Ss6up3Q8iC4LW6g0iVPawcvrZoVBNChxq=eqRg@mail.gmail.com> (raw)
In-Reply-To: <004d01d8abc8$45845920$d08d0b60$@nextmovesoftware.com>

On Tue, Aug 9, 2022 at 10:16 AM Roger Sayle <roger@nextmovesoftware.com> wrote:
>
>
> This x86_64 backend patch allows TImode STV to take advantage of the
> fact that the PTEST instruction performs an AND operation.  Previously
> PTEST was (mostly) used for comparison against zero, by using the same
> operands.  The benefits are demonstrated by the new test case:
>
> __int128 a,b;
> int foo()
> {
>   return (a & b) != 0;
> }
>
> Currently with -O2 -msse4 we generate:
>
>         movdqa  a(%rip), %xmm0
>         pand    b(%rip), %xmm0
>         xorl    %eax, %eax
>         ptest   %xmm0, %xmm0
>         setne   %al
>         ret
>
> with this patch we now generate:
>
>         movdqa  a(%rip), %xmm0
>         xorl    %eax, %eax
>         ptest   b(%rip), %xmm0
>         setne   %al
>         ret
>
> Technically, the magic happens using new define_insn_and_split patterns.
> Using two patterns allows this transformation to performed independently
> of whether TImode STV is run before or after combine.  The one tricky
> case is that immediate constant operands of the AND behave slightly
> differently between TImode and V1TImode: All V1TImode immediate operands
> becomes loads, but for TImode only values that are not hilo_operands
> need to be loaded.  Hence the new *testti_doubleword accepts any
> general_operand, but internally during split calls force_reg whenever
> the second operand is not x86_64_hilo_general_operand.  This required
> (benefits from) some tweaks to TImode STV to support CONST_WIDE_INT in
> more places, using CONST_SCALAR_INT_P instead of just CONST_INT_P.
>
> This patch has been tested on x86_64-pc-linux-gnu with make bootstrap
> and make -k check, both with and without --target_board=unix{-m32},
> with no new failures.  Ok for mainline?
>
>
> 2022-08-09  Roger Sayle  <roger@nextmovesoftware.com>
>
> gcc/ChangeLog
>         * config/i386/i386-features.cc (scalar_chain::convert_compare):
>         Create new pseudos only when/if needed.  Add support for TEST
>         (i.e. (COMPARE (AND x y) (const_int 0)), using UNSPEC_PTEST.
>         When broadcasting V2DImode and V4SImode use new pseudo register.
>         (timode_scalar_chain::convert_op): Do nothing if operand is
>         already V1TImode.  Avoid generating useless SUBREG conversions,
>         i.e. (SUBREG:V1TImode (REG:V1TImode) 0).  Handle CONST_WIDE_INT
>         in addition to CONST_INT by using CONST_SCALAR_INT_P.
>         (convertible_comparison_p): Use CONST_SCALAR_INT_P to match both
>         CONST_WIDE_INT and CONST_INT.  Recognize new *testti_doubleword
>         pattern as an STV candidate.
>         (timode_scalar_to_vector_candidate_p): Allow CONST_SCALAR_INT_P
>         operands in binary logic operations.
>
>         * config/i386/i386.cc (ix86_rtx_costs) <case UNSPEC>: Add costs
>         for UNSPEC_PTEST; a PTEST that performs an AND has the same cost
>         as regular PTEST, i.e. cost->sse_op.
>
>         * config/i386/i386.md (*testti_doubleword): New pre-reload
>         define_insn_and_split that recognizes comparison of TI mode AND
>         against zero.
>         * config/i386/sse.md (*ptest<mode>_and): New pre-reload
>         define_insn_and_split that recognizes UNSPEC_PTEST of identical
>         AND operands.
>
> gcc/testsuite/ChangeLog
>         * gcc.target/i386/sse4_1-stv-8.c: New test case.

OK.

BTW, does your patch also handle DImode and SImode comparisons? They
can be implemented with PTEST, and perhaps they could benefit from
embedded AND, too.

Thanks,
Uros.

>
>
> Thanks in advance,
> Roger
> --
>

      reply	other threads:[~2022-08-09  8:43 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-09  8:16 Roger Sayle
2022-08-09  8:43 ` Uros Bizjak [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAFULd4brLgO0Ss6up3Q8iC4LW6g0iVPawcvrZoVBNChxq=eqRg@mail.gmail.com' \
    --to=ubizjak@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=roger@nextmovesoftware.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).