public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] [RISC-V] optimize Zicond conditional select cases.
@ 2024-04-15  6:33 Fei Gao
  2024-04-15 12:58 ` Kito Cheng
  0 siblings, 1 reply; 6+ messages in thread
From: Fei Gao @ 2024-04-15  6:33 UTC (permalink / raw)
  To: gcc-patches; +Cc: kito.cheng, palmer, jeffreyalaw, Fei Gao

When one of the two input operands is 0, ADD and IOR are functionally
equivalent.
ADD is slightly preferred over IOR because ADD has a higher likelihood
of being implemented as a compressed instruction when compared to IOR.
C.ADD uses the CR format with any of the 32 RVI registers availble,
while C.OR uses the CA format with limit to just 8 of them.

Conditional select, if zero case:
rd = (rc == 0) ? rs1 : rs2

before patch:

  czero.nez rd, rs1, rc
  czero.eqz rtmp, rs2, rc
  or rd, rd, rtmp

after patch:

  czero.eqz rd, rs1, rc
  czero.nez rtmp, rs2, rc
  add rd, rd, rtmp

Same trick applies for the conditional select, if non-zero case:
rd = (rc != 0) ? rs1 : rs2

riscv-gnu-toolchain regression tests have been passed with no new failure.
---
 gcc/config/riscv/riscv.cc                        |  2 +-
 .../gcc.target/riscv/zicond-prefer-add-to-or.c   | 16 ++++++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.target/riscv/zicond-prefer-add-to-or.c

diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index e5f00806bb9..93c736549c9 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -4709,7 +4709,7 @@ riscv_expand_conditional_move (rtx dest, rtx op, rtx cons, rtx alt)
 				  gen_rtx_IF_THEN_ELSE (mode, cond1,
 							CONST0_RTX (mode),
 							alt)));
-	  riscv_emit_binary (IOR, dest, reg1, reg2);
+	  riscv_emit_binary (PLUS, dest, reg1, reg2);
 	  return true;
 	}
     }
diff --git a/gcc/testsuite/gcc.target/riscv/zicond-prefer-add-to-or.c b/gcc/testsuite/gcc.target/riscv/zicond-prefer-add-to-or.c
new file mode 100644
index 00000000000..f3f7beb0b5e
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zicond-prefer-add-to-or.c
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc_zicond -mabi=lp64d -mbranch-cost=4" { target { rv64 } } } */
+/* { dg-options "-march=rv32gc_zicond -mabi=ilp32f -mbranch-cost=4" { target { rv32 } } } */
+/* { dg-skip-if "" { *-*-* } {"-O0" "-Og" "-Os" "-Oz"} } */
+
+long cond_select_if_zero(long a, long b, long c) {
+  return a == 0 ? c : b;
+}
+
+long cond_select_if_non_zero(long a, long b, long c) {
+  return a != 0 ? c : b;
+}
+
+/* { dg-final { scan-assembler-times {add\t}  2 } } */
+/* { dg-final { scan-assembler-not {or\t} } } */
+
-- 
2.17.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] [RISC-V] optimize Zicond conditional select cases.
  2024-04-15  6:33 [PATCH] [RISC-V] optimize Zicond conditional select cases Fei Gao
@ 2024-04-15 12:58 ` Kito Cheng
  2024-04-15 13:04   ` Jeff Law
  0 siblings, 1 reply; 6+ messages in thread
From: Kito Cheng @ 2024-04-15 12:58 UTC (permalink / raw)
  To: Fei Gao; +Cc: GCC Patches, Palmer Dabbelt, Jeff Law

[-- Attachment #1: Type: text/plain, Size: 2812 bytes --]

It's simple enough, so LGTM for trunk :)

Fei Gao <gaofei@eswincomputing.com> 於 2024年4月15日 週一 14:38 寫道:

> When one of the two input operands is 0, ADD and IOR are functionally
> equivalent.
> ADD is slightly preferred over IOR because ADD has a higher likelihood
> of being implemented as a compressed instruction when compared to IOR.
> C.ADD uses the CR format with any of the 32 RVI registers availble,
> while C.OR uses the CA format with limit to just 8 of them.
>
> Conditional select, if zero case:
> rd = (rc == 0) ? rs1 : rs2
>
> before patch:
>
>   czero.nez rd, rs1, rc
>   czero.eqz rtmp, rs2, rc
>   or rd, rd, rtmp
>
> after patch:
>
>   czero.eqz rd, rs1, rc
>   czero.nez rtmp, rs2, rc
>   add rd, rd, rtmp
>
> Same trick applies for the conditional select, if non-zero case:
> rd = (rc != 0) ? rs1 : rs2
>
> riscv-gnu-toolchain regression tests have been passed with no new failure.
> ---
>  gcc/config/riscv/riscv.cc                        |  2 +-
>  .../gcc.target/riscv/zicond-prefer-add-to-or.c   | 16 ++++++++++++++++
>  2 files changed, 17 insertions(+), 1 deletion(-)
>  create mode 100644
> gcc/testsuite/gcc.target/riscv/zicond-prefer-add-to-or.c
>
> diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
> index e5f00806bb9..93c736549c9 100644
> --- a/gcc/config/riscv/riscv.cc
> +++ b/gcc/config/riscv/riscv.cc
> @@ -4709,7 +4709,7 @@ riscv_expand_conditional_move (rtx dest, rtx op, rtx
> cons, rtx alt)
>                                   gen_rtx_IF_THEN_ELSE (mode, cond1,
>                                                         CONST0_RTX (mode),
>                                                         alt)));
> -         riscv_emit_binary (IOR, dest, reg1, reg2);
> +         riscv_emit_binary (PLUS, dest, reg1, reg2);
>           return true;
>         }
>      }
> diff --git a/gcc/testsuite/gcc.target/riscv/zicond-prefer-add-to-or.c
> b/gcc/testsuite/gcc.target/riscv/zicond-prefer-add-to-or.c
> new file mode 100644
> index 00000000000..f3f7beb0b5e
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/zicond-prefer-add-to-or.c
> @@ -0,0 +1,16 @@
> +/* { dg-do compile } */
> +/* { dg-options "-march=rv64gc_zicond -mabi=lp64d -mbranch-cost=4" {
> target { rv64 } } } */
> +/* { dg-options "-march=rv32gc_zicond -mabi=ilp32f -mbranch-cost=4" {
> target { rv32 } } } */
> +/* { dg-skip-if "" { *-*-* } {"-O0" "-Og" "-Os" "-Oz"} } */
> +
> +long cond_select_if_zero(long a, long b, long c) {
> +  return a == 0 ? c : b;
> +}
> +
> +long cond_select_if_non_zero(long a, long b, long c) {
> +  return a != 0 ? c : b;
> +}
> +
> +/* { dg-final { scan-assembler-times {add\t}  2 } } */
> +/* { dg-final { scan-assembler-not {or\t} } } */
> +
> --
> 2.17.1
>
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] [RISC-V] optimize Zicond conditional select cases.
  2024-04-15 12:58 ` Kito Cheng
@ 2024-04-15 13:04   ` Jeff Law
  2024-04-16  1:27     ` Fei Gao
  0 siblings, 1 reply; 6+ messages in thread
From: Jeff Law @ 2024-04-15 13:04 UTC (permalink / raw)
  To: Kito Cheng, Fei Gao; +Cc: GCC Patches, Palmer Dabbelt



On 4/15/24 6:58 AM, Kito Cheng wrote:
> It's simple enough, so LGTM for trunk :)
We're already doing this internally.  I just hadn't submitted it due to 
being deep into stage4.

Jeff


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Re: [PATCH] [RISC-V] optimize Zicond conditional select cases.
  2024-04-15 13:04   ` Jeff Law
@ 2024-04-16  1:27     ` Fei Gao
  2024-04-16  2:05       ` Jeff Law
  0 siblings, 1 reply; 6+ messages in thread
From: Fei Gao @ 2024-04-16  1:27 UTC (permalink / raw)
  To: jeffreyalaw, Kito Cheng; +Cc: gcc-patches, Palmer Dabbelt

On 2024-04-15 21:04  Jeff Law <jeffreyalaw@gmail.com> wrote:
>
>
>
>On 4/15/24 6:58 AM, Kito Cheng wrote:
>> It's simple enough, so LGTM for trunk :)
>We're already doing this internally.  I just hadn't submitted it due to
>being deep into stage4.
>
>Jeff 

Hi Jeff

Would you like me to commit it now or leave it to you with your bunch of optimizations in Zicond?

BR
Fei

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] [RISC-V] optimize Zicond conditional select cases.
  2024-04-16  1:27     ` Fei Gao
@ 2024-04-16  2:05       ` Jeff Law
  2024-04-16  5:50         ` Fei Gao
  0 siblings, 1 reply; 6+ messages in thread
From: Jeff Law @ 2024-04-16  2:05 UTC (permalink / raw)
  To: Fei Gao, Kito Cheng; +Cc: gcc-patches, Palmer Dabbelt



On 4/15/24 7:27 PM, Fei Gao wrote:
> On 2024-04-15 21:04  Jeff Law <jeffreyalaw@gmail.com> wrote:
>>
>>
>>
>> On 4/15/24 6:58 AM, Kito Cheng wrote:
>>> It's simple enough, so LGTM for trunk :)
>> We're already doing this internally.  I just hadn't submitted it due to
>> being deep into stage4.
>>
>> Jeff
> 
> Hi Jeff
> 
> Would you like me to commit it now or leave it to you with your bunch of optimizations in Zicond?
Go ahead and commit it.  It's extremely low risk.

jeff


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] [RISC-V] optimize Zicond conditional select cases.
  2024-04-16  2:05       ` Jeff Law
@ 2024-04-16  5:50         ` Fei Gao
  0 siblings, 0 replies; 6+ messages in thread
From: Fei Gao @ 2024-04-16  5:50 UTC (permalink / raw)
  To: jeffreyalaw, Kito Cheng; +Cc: gcc-patches, Palmer Dabbelt

Committed. Thanks Kito and Jeff for the reveiw. 

BR
Fei
>
>
>On 4/15/24 7:27 PM, Fei Gao wrote:
>> On 2024-04-15 21:04  Jeff Law <jeffreyalaw@gmail.com> wrote:
>>>
>>>
>>>
>>> On 4/15/24 6:58 AM, Kito Cheng wrote:
>>>> It's simple enough, so LGTM for trunk :)
>>> We're already doing this internally.  I just hadn't submitted it due to
>>> being deep into stage4.
>>>
>>> Jeff
>>
>> Hi Jeff
>>
>> Would you like me to commit it now or leave it to you with your bunch of optimizations in Zicond?
>Go ahead and commit it.  It's extremely low risk.
>
>jeff

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-04-16  5:50 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-15  6:33 [PATCH] [RISC-V] optimize Zicond conditional select cases Fei Gao
2024-04-15 12:58 ` Kito Cheng
2024-04-15 13:04   ` Jeff Law
2024-04-16  1:27     ` Fei Gao
2024-04-16  2:05       ` Jeff Law
2024-04-16  5:50         ` Fei Gao

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).