public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* RISC-V: Replace not + bitwise_imm with li + bitwise_not
@ 2023-09-11 16:43 Jivan Hakobyan
  2023-09-11 19:16 ` Andrew Waterman
  0 siblings, 1 reply; 3+ messages in thread
From: Jivan Hakobyan @ 2023-09-11 16:43 UTC (permalink / raw)
  To: GCC Patches


[-- Attachment #1.1: Type: text/plain, Size: 771 bytes --]

In the case when we have C code like this

int foo (int a) {
   return 100 & ~a;
}

GCC generates the following instruction sequence

foo:
     not     a0,a0
     andi    a0,a0,100
     ret

This patch replaces that with this sequence
foo:
     li a5,100
     andn a0,a5,a0
     ret

The profitability comes from an out-of-order processor being able to
issue the "li a5, 100" at any time after it's fetched while "not a0, a0" has
to wait until any prior setter of a0 has reached completion.


gcc/ChangeLog:
        * config/riscv/bitmanip.md (*<optab>_not_const<mode>): New split
pattern.

gcc/testsuite/ChangeLog:
        * gcc.target/riscv/zbb-andn-orn-01.c: New test.
        * gcc.target/riscv/zbb-andn-orn-02.c: Likewise.


-- 
With the best regards
Jivan Hakobyan

[-- Attachment #2: bitwise_not.diff --]
[-- Type: text/x-patch, Size: 2275 bytes --]

diff --git a/gcc/config/riscv/bitmanip.md b/gcc/config/riscv/bitmanip.md
index 7b55528ee49..209b0afb118 100644
--- a/gcc/config/riscv/bitmanip.md
+++ b/gcc/config/riscv/bitmanip.md
@@ -215,6 +215,17 @@ (define_insn "*<optab>_not<mode>"
   [(set_attr "type" "bitmanip")
    (set_attr "mode" "<X:MODE>")])
 
+(define_insn_and_split "*<optab>_not_const<mode>"
+  [(set (match_operand:X 0 "register_operand" "=r")
+       (bitmanip_bitwise:X (not:X (match_operand:X 1 "register_operand" "r"))
+              (match_operand:X 2 "const_arith_operand" "I")))
+  (clobber (match_scratch:X 3 "=&r"))]
+  "TARGET_ZBB || TARGET_ZBKB"
+  "#"
+  "&& reload_completed"
+  [(set (match_dup 3) (match_dup 2))
+   (set (match_dup 0) (bitmanip_bitwise:X (not:X (match_dup 1)) (match_dup 3)))])
+
 ;; '(a >= 0) ? b : 0' is emitted branchless (from if-conversion).  Without a
 ;; bit of extra help for combine (i.e., the below split), we end up emitting
 ;; not/srai/and instead of combining the not into an andn.
diff --git a/gcc/testsuite/gcc.target/riscv/zbb-andn-orn-01.c b/gcc/testsuite/gcc.target/riscv/zbb-andn-orn-01.c
new file mode 100644
index 00000000000..e1c33885913
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zbb-andn-orn-01.c
@@ -0,0 +1,17 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc_zbb -mabi=lp64" } */
+/* { dg-skip-if "" { *-*-* } { "-O0" "-g" } } */
+
+int foo1(int rs1)
+{
+return 100 & ~rs1;
+}
+
+int foo2(int rs1)
+{
+return 100 | ~rs1;
+}
+
+/* { dg-final { scan-assembler-times "andn\t" 1 } } */
+/* { dg-final { scan-assembler-times "orn\t" 1 } } */
+/* { dg-final { scan-assembler-times "li\t" 2 } } */
diff --git a/gcc/testsuite/gcc.target/riscv/zbb-andn-orn-02.c b/gcc/testsuite/gcc.target/riscv/zbb-andn-orn-02.c
new file mode 100644
index 00000000000..b51950cdb7d
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zbb-andn-orn-02.c
@@ -0,0 +1,17 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv32gc_zbb -mabi=ilp32" } */
+/* { dg-skip-if "" { *-*-* } { "-O0" "-g" } } */
+
+int foo1(int rs1)
+{
+return 100 & ~rs1;
+}
+
+int foo2(int rs1)
+{
+return 100 | ~rs1;
+}
+
+/* { dg-final { scan-assembler-times "andn\t" 1 } } */
+/* { dg-final { scan-assembler-times "orn\t" 1 } } */
+/* { dg-final { scan-assembler-times "li\t" 2 } } */

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

* Re: RISC-V: Replace not + bitwise_imm with li + bitwise_not
  2023-09-11 16:43 RISC-V: Replace not + bitwise_imm with li + bitwise_not Jivan Hakobyan
@ 2023-09-11 19:16 ` Andrew Waterman
  2023-09-12  0:56   ` Jeff Law
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Waterman @ 2023-09-11 19:16 UTC (permalink / raw)
  To: Jivan Hakobyan; +Cc: GCC Patches

Note this is a size-speed tradeoff, as the Zcb extension has a
16-bit-wide C.NOT instruction.  Might want to suppress this
optimization when Zcb is present and the function is being optimized
for size.


On Mon, Sep 11, 2023 at 9:52 AM Jivan Hakobyan via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> In the case when we have C code like this
>
> int foo (int a) {
>    return 100 & ~a;
> }
>
> GCC generates the following instruction sequence
>
> foo:
>      not     a0,a0
>      andi    a0,a0,100
>      ret
>
> This patch replaces that with this sequence
> foo:
>      li a5,100
>      andn a0,a5,a0
>      ret
>
> The profitability comes from an out-of-order processor being able to
> issue the "li a5, 100" at any time after it's fetched while "not a0, a0" has
> to wait until any prior setter of a0 has reached completion.
>
>
> gcc/ChangeLog:
>         * config/riscv/bitmanip.md (*<optab>_not_const<mode>): New split
> pattern.
>
> gcc/testsuite/ChangeLog:
>         * gcc.target/riscv/zbb-andn-orn-01.c: New test.
>         * gcc.target/riscv/zbb-andn-orn-02.c: Likewise.
>
>
> --
> With the best regards
> Jivan Hakobyan

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

* Re: RISC-V: Replace not + bitwise_imm with li + bitwise_not
  2023-09-11 19:16 ` Andrew Waterman
@ 2023-09-12  0:56   ` Jeff Law
  0 siblings, 0 replies; 3+ messages in thread
From: Jeff Law @ 2023-09-12  0:56 UTC (permalink / raw)
  To: Andrew Waterman, Jivan Hakobyan; +Cc: GCC Patches



On 9/11/23 13:16, Andrew Waterman via Gcc-patches wrote:
> Note this is a size-speed tradeoff, as the Zcb extension has a
> 16-bit-wide C.NOT instruction.  Might want to suppress this
> optimization when Zcb is present and the function is being optimize > for size.
Yea, let's gate this on !optimize_function_for_size_p (cfun).

While we don't have Zcb support yet, hopefully we will soon.

jeff

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

end of thread, other threads:[~2023-09-12  0:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-11 16:43 RISC-V: Replace not + bitwise_imm with li + bitwise_not Jivan Hakobyan
2023-09-11 19:16 ` Andrew Waterman
2023-09-12  0:56   ` Jeff Law

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