From: Philipp Tomsich <philipp.tomsich@vrull.eu>
To: Kito Cheng <kito.cheng@gmail.com>
Cc: Jeff Law <jlaw@ventanamicro.com>,
Jeff Law <jeffreyalaw@gmail.com>,
gcc-patches@gcc.gnu.org,
Christoph Muellner <christoph.muellner@vrull.eu>,
Palmer Dabbelt <palmer@rivosinc.com>,
Vineet Gupta <vineetg@rivosinc.com>
Subject: Re: [PATCH] RISC-V: Optimise adding a (larger than simm12) constant
Date: Mon, 21 Nov 2022 11:13:53 +0100 [thread overview]
Message-ID: <CAAeLtUDrKh-QMi=cHbAD_+e4SZgueeZ=K+eD9hsuzK2UL7sf7g@mail.gmail.com> (raw)
In-Reply-To: <CA+yXCZCSRQyji0bScnrrbtSbwOBG2ax6wQjeQ-n9mVaQUA2jOw@mail.gmail.com>
On Mon, 21 Nov 2022 at 04:11, Kito Cheng <kito.cheng@gmail.com> wrote:
>
> > @@ -464,6 +464,60 @@
> > [(set_attr "type" "arith")
> > (set_attr "mode" "DI")])
> >
> > +(define_expand "add<mode>3"
> > + [(set (match_operand:GPR 0 "register_operand" "=r,r")
> > + (plus:GPR (match_operand:GPR 1 "register_operand" " r,r")
> > + (match_operand:GPR 2 "addi_operand" " r,I")))]
>
> Is it possible to just define a predicate that accepts
> register_operand and CONST_INT_P,
> and then handle all cases in add<mode>3 pattern?
Great suggestion.
>
> My point is put all check in one place:
>
> e.g.
> check TARGET_ZBA && const_arith_shifted123_operand (operands[2],
> <MODE>mode) in add<mode>3
> rather than check TARGET_ZBA in addi_operand and use sh[123]add in
> add<mode>3 without check.
>
> and that also means we need to sync addi_opearnad and add<mode>3
> once we have extension XX could improve addi codegen.
>
>
> > + ""
> > +{
> > + if (arith_operand (operands[2], <MODE>mode))
> > + emit_insn (gen_riscv_add<mode>3 (operands[0], operands[1], operands[2]));
> > + else if (const_arith_2simm12_operand (operands[2], <MODE>mode))
>
> const_arith_2simm12_operand only used once, could you inline the condition here?
If we handle all cases in a single pattern, we'll punt this to riscv.cc anyway.
So let's see how the code looks once we have a single predicate and do
the inlining there...
>
> > + {
> > + /* Split into two immediates that add up to the desired value:
> > + * e.g., break up "a + 2445" into:
> > + * addi a0,a0,2047
> > + * addi a0,a0,398
> > + */
> > +
> > + HOST_WIDE_INT val = INTVAL (operands[2]);
> > + HOST_WIDE_INT saturated = HOST_WIDE_INT_M1U << (IMM_BITS - 1);
> > +
> > + if (val >= 0)
> > + saturated = ~saturated;
> > +
> > + val -= saturated;
> > +
> > + rtx tmp = gen_reg_rtx (<MODE>mode);
> > + emit_insn (gen_riscv_add<mode>3 (tmp, operands[1], GEN_INT (saturated)));
> > + emit_insn (gen_riscv_add<mode>3 (operands[0], tmp, GEN_INT (val)));
> > + }
> > + else if (<MODE>mode == word_mode
> > + && const_arith_shifted123_operand (operands[2], <MODE>mode))
>
> Same for const_arith_shifted123_operand.
>
> > + {
> > + /* Use a sh[123]add and an immediate shifted down by 1, 2, or 3. */
> > +
> > + HOST_WIDE_INT val = INTVAL (operands[2]);
> > + int shamt = ctz_hwi (val);
> > +
> > + if (shamt > 3)
> > + shamt = 3;
> > +
> > + rtx tmp = gen_reg_rtx (<MODE>mode);
> > + emit_insn (gen_rtx_SET (tmp, GEN_INT (val >> shamt)));
> > +
> > + /* We don't use gen_riscv_shNadd here, as it will only exist for
> > + <X:mode>. Instead we build up its canonical form directly. */
> > + rtx shifted_imm = gen_rtx_ASHIFT (<MODE>mode, tmp, GEN_INT (shamt));
> > + rtx shNadd = gen_rtx_PLUS (<MODE>mode, shifted_imm, operands[1]);
> > + emit_insn (gen_rtx_SET (operands[0], shNadd));
> > + }
> > + else
> > + FAIL;
>
> Seems add<mode>3 FAIL will cause problems, we need either add something like:
>
> operands[2] = force_reg (<MODE>mode, operands[2]);
> emit_insn (gen_rtx_SET (operands[0],
> gen_rtx_PLUS (<MODE>mode,
> operands[1], operands[2])));
>
> Or just gcc_unreachable () if we keep using addi_operand to guard this pattern.
This is a case for "gcc_unreachable ();".
The change will be in v2.
Thanks,
Philipp.
prev parent reply other threads:[~2022-11-21 10:14 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-09 23:07 Philipp Tomsich
2022-11-18 21:13 ` Jeff Law
2022-11-18 21:26 ` Philipp Tomsich
2022-11-18 22:14 ` Jeff Law
2022-11-21 3:10 ` Kito Cheng
2022-11-21 10:13 ` Philipp Tomsich [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='CAAeLtUDrKh-QMi=cHbAD_+e4SZgueeZ=K+eD9hsuzK2UL7sf7g@mail.gmail.com' \
--to=philipp.tomsich@vrull.eu \
--cc=christoph.muellner@vrull.eu \
--cc=gcc-patches@gcc.gnu.org \
--cc=jeffreyalaw@gmail.com \
--cc=jlaw@ventanamicro.com \
--cc=kito.cheng@gmail.com \
--cc=palmer@rivosinc.com \
--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).