public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <richard.guenther@gmail.com>
To: Philipp Tomsich <philipp.tomsich@vrull.eu>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>,
	Jim Wilson <wilson@tuliptree.org>,
	 Kito Cheng <kito.cheng@gmail.com>
Subject: Re: [PATCH v1 1/8] bswap: synthesize HImode bswap from SImode or DImode
Date: Fri, 19 Nov 2021 11:20:24 +0100	[thread overview]
Message-ID: <CAFiYyc23ctJqaFB_rTP3QB43byA0K40KfP2LM6iSnaRS9iuokg@mail.gmail.com> (raw)
In-Reply-To: <20211111141020.2738001-2-philipp.tomsich@vrull.eu>

On Thu, Nov 11, 2021 at 3:13 PM Philipp Tomsich
<philipp.tomsich@vrull.eu> wrote:
>
> The RISC-V Zbb extension adds an XLEN (i.e. SImode for rv32, DImode
> for rv64) bswap instruction (rev8).  While, with the current master,
> SImode is synthesized correctly from DImode, HImode is not.
>
> This change adds an appropriate expansion for a HImode bswap, if a
> wider bswap is available.
>
> Without this change, the following rv64gc_zbb code is generated for
> __builtin_bswap16():
>         slliw   a5,a0,8
>         zext.h  a0,a0
>         srliw   a0,a0,8
>         or      a0,a5,a0
>         sext.h  a0,a0      // this is a 16bit sign-extension following
>                            // the byteswap (e.g. on a 'short' function
>                            // return).
>
> After this change, a bswap (rev8) is used and any extensions are
> combined into the shift-right:
>         rev8    a0,a0
>         srai    a0,a0,48   // the sign-extension is combined into the
>                            // shift; a srli is emitted otherwise...
>
> gcc/ChangeLog:
>
>         * optabs.c (expand_unop): support expanding a HImode bswap
>           using SImode or DImode, followed by a shift.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.target/riscv/zbb-bswap.c: New test.
>
> Signed-off-by: Philipp Tomsich <philipp.tomsich@vrull.eu>
> ---
>
>  gcc/optabs.c                               |  6 ++++++
>  gcc/testsuite/gcc.target/riscv/zbb-bswap.c | 22 ++++++++++++++++++++++
>  2 files changed, 28 insertions(+)
>  create mode 100644 gcc/testsuite/gcc.target/riscv/zbb-bswap.c
>
> diff --git a/gcc/optabs.c b/gcc/optabs.c
> index 019bbb62882..7a3ffbe4525 100644
> --- a/gcc/optabs.c
> +++ b/gcc/optabs.c
> @@ -3307,6 +3307,12 @@ expand_unop (machine_mode mode, optab unoptab, rtx op0, rtx target,
>                 return temp;
>             }
>
> +         /* If we are missing a HImode BSWAP, but have one for SImode or
> +            DImode, use a BSWAP followed by a SHIFT.  */
> +         temp = widen_bswap (as_a <scalar_int_mode> (mode), op0, target);
> +         if (temp)
> +           return temp;
> +

I think it would be more natural to temporarily terminate the HImode case here
and re-open it inside the following

      if (is_a <scalar_int_mode> (mode, &int_mode))
        {
          temp = widen_bswap (int_mode, op0, target);
          if (temp)
            return temp;

here to handle the ashl/lshr/ior fallback.

Richard.

>           last = get_last_insn ();
>
>           temp1 = expand_binop (mode, ashl_optab, op0,
> diff --git a/gcc/testsuite/gcc.target/riscv/zbb-bswap.c b/gcc/testsuite/gcc.target/riscv/zbb-bswap.c
> new file mode 100644
> index 00000000000..6ee27d9f47a
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/zbb-bswap.c
> @@ -0,0 +1,22 @@
> +/* { dg-do compile } */
> +/* { dg-options "-march=rv64gc_zbb -mabi=lp64 -O2" } */
> +
> +unsigned long
> +func64 (unsigned long i)
> +{
> +  return __builtin_bswap64(i);
> +}
> +
> +unsigned int
> +func32 (unsigned int i)
> +{
> +  return __builtin_bswap32(i);
> +}
> +
> +unsigned short
> +func16 (unsigned short i)
> +{
> +  return __builtin_bswap16(i);
> +}
> +
> +/* { dg-final { scan-assembler-times "rev8" 3 } } */
> --
> 2.32.0
>

  parent reply	other threads:[~2021-11-19 10:20 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-11 14:10 [PATCH v1 0/8] Improvements to bitmanip-1.0 (Zb[abcs]) support Philipp Tomsich
2021-11-11 14:10 ` [PATCH v1 1/8] bswap: synthesize HImode bswap from SImode or DImode Philipp Tomsich
2021-11-17 14:51   ` Kito Cheng
2021-11-19 10:20   ` Richard Biener [this message]
2021-11-19 10:21     ` Richard Biener
2021-11-11 14:10 ` [PATCH v1 2/8] RISC-V: costs: handle BSWAP Philipp Tomsich
2021-11-17 14:52   ` Kito Cheng
2021-11-11 14:10 ` [PATCH v1 3/8] RISC-V: costs: support shift-and-add in strength-reduction Philipp Tomsich
2021-11-11 14:10 ` [PATCH v1 4/8] RISC-V: bitmanip: fix constant-loading for (1ULL << 31) in DImode Philipp Tomsich
2021-11-11 14:10 ` [PATCH v1 5/8] RISC-V: bitmanip: improvements to rotate instructions Philipp Tomsich
2021-11-11 14:10 ` [PATCH v1 6/8] RISC-V: bitmanip: add splitter to use bexti for "(a & (1 << BIT_NO)) ? 0 : -1" Philipp Tomsich
2021-11-18  9:45   ` Kito Cheng
2021-11-11 14:10 ` [PATCH v1 7/8] RISC-V: bitmanip: add orc.b as an unspec Philipp Tomsich
2021-11-11 14:10 ` [PATCH v1 8/8] RISC-V: bitmanip: relax minmax to operate on GPR Philipp Tomsich
2021-11-11 16:00   ` Kito Cheng
2021-11-11 16:18     ` Philipp Tomsich
2021-11-11 16:27       ` Kito Cheng
2021-11-11 16:42         ` Kito Cheng
2021-11-11 18:33           ` Philipp Tomsich

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=CAFiYyc23ctJqaFB_rTP3QB43byA0K40KfP2LM6iSnaRS9iuokg@mail.gmail.com \
    --to=richard.guenther@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=kito.cheng@gmail.com \
    --cc=philipp.tomsich@vrull.eu \
    --cc=wilson@tuliptree.org \
    /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).