From: Andrew Pinski <pinskia@gmail.com>
To: Palmer Dabbelt <palmer@dabbelt.com>
Cc: gcc-patches@gcc.gnu.org
Subject: Re: RISC-V: Add divmod instruction support
Date: Sat, 18 Feb 2023 10:42:51 -0800 [thread overview]
Message-ID: <CA+=Sn1m3TBcuPPLhy+EOyejAfKaK6SaD-8LeBqYdNCLzp_K2PA@mail.gmail.com> (raw)
In-Reply-To: <mhng-80df8fcb-f95d-4060-bbd1-a14cb4854250@palmer-ri-x1c9a>
On Sat, Feb 18, 2023 at 10:27 AM Palmer Dabbelt <palmer@dabbelt.com> wrote:
>
> On Fri, 17 Feb 2023 06:02:40 PST (-0800), gcc-patches@gcc.gnu.org wrote:
> > Hi all,
> > If we have division and remainder calculations with the same operands:
> >
> > a = b / c;
> > d = b % c;
> >
> > We can replace the calculation of remainder with multiplication +
> > subtraction, using the result from the previous division:
> >
> > a = b / c;
> > d = a * c;
> > d = b - d;
> >
> > Which will be faster.
>
> Do you have any benchmarks that show that performance increase? The ISA
> manual specifically says the suggested sequence is div+mod, and while
> those suggestions don't always pan out for real hardware it's likely
> that at least some implementations will end up with the ISA-suggested
> fusions.
I suspect I will be needing this kind of patch for the core that I am
going to be using.
If anything this should be under a tuning option.
Thanks,
Andrew Pinski
>
> > Currently, it isn't done for RISC-V.
> >
> > I've added an expander for DIVMOD which replaces 'rem' with 'mul + sub'.
> >
> > Best regards,
> > Matevos.
> >
> > gcc/ChangeLog:
> >
> > * config/riscv/riscv.md: Added divmod expander.
> >
> > gcc/testsuite/ChangeLog:
> > * gcc.target/riscv/divmod.c: New testcase.
> >
> > --- inline copy of the patch ---
> >
> > diff --git a/gcc/config/riscv/iterators.md b/gcc/config/riscv/iterators.md
> > index f95dd405e12..d941483d9f1 100644
> > --- a/gcc/config/riscv/iterators.md
> > +++ b/gcc/config/riscv/iterators.md
> > @@ -148,6 +148,11 @@
> > ;; from the same template.
> > (define_code_iterator any_mod [mod umod])
> >
> > +;; These code iterators allow unsigned and signed divmod to be generated
> > +;; from the same template.
> > +(define_code_iterator only_div [div udiv])
> > +(define_code_attr paired_mod [(div "mod") (udiv "umod")])
> > +
> > ;; These code iterators allow the signed and unsigned scc operations to use
> > ;; the same template.
> > (define_code_iterator any_gt [gt gtu])
> > @@ -175,7 +180,8 @@
> > (gt "") (gtu "u")
> > (ge "") (geu "u")
> > (lt "") (ltu "u")
> > - (le "") (leu "u")])
> > + (le "") (leu "u")
> > + (div "") (udiv "u")])
> >
> > ;; <su> is like <u>, but the signed form expands to "s" rather than "".
> > (define_code_attr su [(sign_extend "s") (zero_extend "u")])
> > diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md
> > index c8adc5af5d2..2d48ff3f8de 100644
> > --- a/gcc/config/riscv/riscv.md
> > +++ b/gcc/config/riscv/riscv.md
> > @@ -1044,6 +1044,22 @@
> > [(set_attr "type" "idiv")
> > (set_attr "mode" "DI")])
> >
> > +(define_expand "<u>divmod<mode>4"
> > + [(parallel
> > + [(set (match_operand:GPR 0 "register_operand")
> > + (only_div:GPR (match_operand:GPR 1 "register_operand")
> > + (match_operand:GPR 2 "register_operand")))
> > + (set (match_operand:GPR 3 "register_operand")
> > + (<paired_mod>:GPR (match_dup 1) (match_dup 2)))])]
> > + "TARGET_DIV"
> > + {
> > + rtx tmp = gen_reg_rtx (<MODE>mode);
> > + emit_insn (gen_<u>div<GPR:mode>3 (operands[0], operands[1],
> > operands[2]));
> > + emit_insn (gen_mul<GPR:mode>3 (tmp, operands[0], operands[2]));
> > + emit_insn (gen_sub<GPR:mode>3 (operands[3], operands[1], tmp));
> > + DONE;
> > + })
> > +
> > (define_insn "*<optab>si3_extended"
> > [(set (match_operand:DI 0 "register_operand" "=r")
> > (sign_extend:DI
> > diff --git a/gcc/testsuite/gcc.target/riscv/divmod.c
> > b/gcc/testsuite/gcc.target/riscv/divmod.c
> > new file mode 100644
> > index 00000000000..254b25e654d
> > --- /dev/null
> > +++ b/gcc/testsuite/gcc.target/riscv/divmod.c
> > @@ -0,0 +1,14 @@
> > +/* { dg-do compile } */
> > +/* { dg-skip-if "" { *-*-* } { "-O0" "-O1" "-Og" } } */
> > +
> > +void
> > +foo(int a, int b, int *c, int *d)
> > +{
> > + *c = a / b;
> > + *d = a % b;
> > +}
> > +
> > +/* { dg-final { scan-assembler-not "rem" } } */
> > +/* { dg-final { scan-assembler-times "mul" 1 } } */
> > +/* { dg-final { scan-assembler-times "sub" 1 } } */
next prev parent reply other threads:[~2023-02-18 18:43 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-17 14:02 Matevos Mehrabyan
2023-02-18 18:26 ` Palmer Dabbelt
2023-02-18 18:42 ` Andrew Pinski [this message]
2023-02-18 19:26 ` Palmer Dabbelt
2023-02-18 19:31 ` Maciej W. Rozycki
2023-02-18 20:57 ` Prathamesh Kulkarni
2023-02-18 21:07 ` Jeff Law
2023-02-19 1:14 ` Maciej W. Rozycki
2023-02-20 8:11 ` Richard Biener
2023-02-20 13:32 ` Alexander Monakov
2023-02-28 12:54 ` Maciej W. Rozycki
2023-02-18 21:06 ` Jeff Law
2023-02-18 21:30 ` Palmer Dabbelt
2023-02-18 21:57 ` Jeff Law
2023-02-20 1:27 ` Andrew Waterman
2023-04-28 20:09 ` Jeff Law
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='CA+=Sn1m3TBcuPPLhy+EOyejAfKaK6SaD-8LeBqYdNCLzp_K2PA@mail.gmail.com' \
--to=pinskia@gmail.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=palmer@dabbelt.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).