From: Jeff Law <jeffreyalaw@gmail.com>
To: Christoph Muellner <christoph.muellner@vrull.eu>,
gcc-patches@gcc.gnu.org, Kito Cheng <kito.cheng@sifive.com>,
Jim Wilson <jim.wilson.gcc@gmail.com>,
Palmer Dabbelt <palmer@dabbelt.com>,
Andrew Waterman <andrew@sifive.com>,
Philipp Tomsich <philipp.tomsich@vrull.eu>,
Vineet Gupta <vineetg@rivosinc.com>
Subject: Re: [PATCH v2 1/2] riscv: Add support for strlen inline expansion
Date: Mon, 11 Sep 2023 21:28:51 -0600 [thread overview]
Message-ID: <c927397d-ab7f-43d5-8f07-6deaf513acd7@gmail.com> (raw)
In-Reply-To: <20230906160734.2422522-2-christoph.muellner@vrull.eu>
On 9/6/23 10:07, Christoph Muellner wrote:
> From: Christoph Müllner <christoph.muellner@vrull.eu>
>
> This patch implements the expansion of the strlen builtin for RV32/RV64
> for xlen-aligned aligned strings if Zbb or XTheadBb instructions are available.
> The inserted sequences are:
>
> rv32gc_zbb (RV64 is similar):
> add a3,a0,4
> li a4,-1
> .L1: lw a5,0(a0)
> add a0,a0,4
> orc.b a5,a5
> beq a5,a4,.L1
> not a5,a5
> ctz a5,a5
> srl a5,a5,0x3
> add a0,a0,a5
> sub a0,a0,a3
>
> rv64gc_xtheadbb (RV32 is similar):
> add a4,a0,8
> .L2: ld a5,0(a0)
> add a0,a0,8
> th.tstnbz a5,a5
> beqz a5,.L2
> th.rev a5,a5
> th.ff1 a5,a5
> srl a5,a5,0x3
> add a0,a0,a5
> sub a0,a0,a4
>
> This allows to inline calls to strlen(), with optimized code for
> xlen-aligned strings, resulting in the following benefits over
> a call to libc:
> * no call/ret instructions
> * no stack frame allocation
> * no register saving/restoring
> * no alignment test
>
> The inlining mechanism is gated by a new switch ('-minline-strlen')
> and by the variable 'optimize_size'.
>
> Tested using the glibc string tests.
>
> Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
>
> gcc/ChangeLog:
>
> * config.gcc: Add new object riscv-string.o.
> riscv-string.cc.
> * config/riscv/riscv-protos.h (riscv_expand_strlen):
> New function.
> * config/riscv/riscv.md (strlen<mode>): New expand INSN.
> * config/riscv/riscv.opt: New flag 'minline-strlen'.
> * config/riscv/t-riscv: Add new object riscv-string.o.
> * config/riscv/thead.md (th_rev<mode>2): Export INSN name.
> (th_rev<mode>2): Likewise.
> (th_tstnbz<mode>2): New INSN.
> * doc/invoke.texi: Document '-minline-strlen'.
> * emit-rtl.cc (emit_likely_jump_insn): New helper function.
> (emit_unlikely_jump_insn): Likewise.
> * rtl.h (emit_likely_jump_insn): New prototype.
> (emit_unlikely_jump_insn): Likewise.
> * config/riscv/riscv-string.cc: New file.
>
> gcc/testsuite/ChangeLog:
>
> * gcc.target/riscv/xtheadbb-strlen-unaligned.c: New test.
> * gcc.target/riscv/xtheadbb-strlen.c: New test.
> * gcc.target/riscv/zbb-strlen-disabled-2.c: New test.
> * gcc.target/riscv/zbb-strlen-disabled.c: New test.
> * gcc.target/riscv/zbb-strlen-unaligned.c: New test.
> * gcc.target/riscv/zbb-strlen.c: New test.
Note that I don't think we need the new UNSPEC_STRLEN since its only
used in the expander and doesn't survive into RTL. Your call on whether
or not to remove it now or as a separate patch (or keep it if I'm wrong
about it not being needed.
OK for the trunk. Sorry this got lost in the shuffle last year.
jeff
.
next prev parent reply other threads:[~2023-09-12 3:28 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-06 16:07 [PATCH v2 0/2] riscv: Introduce strlen/strcmp/strncmp " Christoph Muellner
2023-09-06 16:07 ` [PATCH v2 1/2] riscv: Add support for strlen " Christoph Muellner
2023-09-06 16:22 ` Palmer Dabbelt
2023-09-06 16:47 ` Jeff Law
2023-09-06 19:29 ` Palmer Dabbelt
2023-09-12 3:28 ` Jeff Law [this message]
2023-09-12 9:38 ` Philipp Tomsich
2023-09-06 16:07 ` [PATCH v2 2/2] riscv: Add support for str(n)cmp " Christoph Muellner
2023-09-12 3:34 ` Jeff Law
2023-09-12 9:38 ` 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=c927397d-ab7f-43d5-8f07-6deaf513acd7@gmail.com \
--to=jeffreyalaw@gmail.com \
--cc=andrew@sifive.com \
--cc=christoph.muellner@vrull.eu \
--cc=gcc-patches@gcc.gnu.org \
--cc=jim.wilson.gcc@gmail.com \
--cc=kito.cheng@sifive.com \
--cc=palmer@dabbelt.com \
--cc=philipp.tomsich@vrull.eu \
--cc=vineetg@rivosinc.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).