[1/4,RISC-V] prefer Zicond primitive semantics to SFB

Message ID 20231128023227.36200-1-gaofei@eswincomputing.com
State Committed
Commit fc98a41c8def1cab4f9b8359be84e8623eab88e2
Delegated to: Kito Cheng
Headers
Series [1/4,RISC-V] prefer Zicond primitive semantics to SFB |

Checks

Context Check Description
rivoscibot/toolchain-ci-rivos-lint warning Lint failed
rivoscibot/toolchain-ci-rivos-apply-patch success Patch applied
rivoscibot/toolchain-ci-rivos-build--newlib-rv64gcv-lp64d-multilib success Build passed
rivoscibot/toolchain-ci-rivos-build--linux-rv64gcv-lp64d-multilib success Build passed
rivoscibot/toolchain-ci-rivos-build--newlib-rv64gc-lp64d-multilib success Build passed
rivoscibot/toolchain-ci-rivos-build--linux-rv64gc_zba_zbb_zbc_zbs-lp64d-non-multilib success Build passed
rivoscibot/toolchain-ci-rivos-build--linux-rv32gc_zba_zbb_zbc_zbs-ilp32d-non-multilib success Build passed

Commit Message

Fei Gao Nov. 28, 2023, 2:32 a.m. UTC
  Move Zicond md files ahead of SFB to recognize Zicond first.

Take the following case for example.

CFLAGS: -mtune=sifive-7-series -march=rv64gc_zicond -mabi=lp64d

long primitiveSemantics_00(long a, long b) { return a == 0 ? 0 : b; }

before patch:
primitiveSemantics_00:
	bne	a0,zero,1f	# movcc
	mv	a1,zero
1:
	mv	a0,a1
	ret

after patch:
primitiveSemantics_00:
	czero.eqz	a0,a1,a0
	ret

Co-authored-by: Xiao Zeng<zengxiao@eswincomputing.com>

gcc/ChangeLog:

        * config/riscv/riscv.md (*mov<GPR:mode><X:mode>cc):move to sfb.md
        * config/riscv/sfb.md: New file.

gcc/testsuite/ChangeLog:

        * gcc.target/riscv/zicond-sfb-primitiveSemantics.c: New test.
---
 gcc/config/riscv/riscv.md                     | 19 +------
 gcc/config/riscv/sfb.md                       | 37 ++++++++++++++
 .../riscv/zicond-sfb-primitiveSemantics.c     | 50 +++++++++++++++++++
 3 files changed, 88 insertions(+), 18 deletions(-)
 create mode 100644 gcc/config/riscv/sfb.md
 create mode 100644 gcc/testsuite/gcc.target/riscv/zicond-sfb-primitiveSemantics.c
  

Comments

Kito Cheng Nov. 28, 2023, 3:09 a.m. UTC | #1
Personally I don't like to play with the pattern order to tweak the
code gen since it kinda introduces implicit relation/rule here, but I
guess the only way to prevent that is to duplicate the pattern for SFB
again, which is not an ideal solution...

Anyway, it's obviously a better code gen, so LGTM :)

On Tue, Nov 28, 2023 at 10:33 AM Fei Gao <gaofei@eswincomputing.com> wrote:
>
> Move Zicond md files ahead of SFB to recognize Zicond first.
>
> Take the following case for example.
>
> CFLAGS: -mtune=sifive-7-series -march=rv64gc_zicond -mabi=lp64d
>
> long primitiveSemantics_00(long a, long b) { return a == 0 ? 0 : b; }
>
> before patch:
> primitiveSemantics_00:
>         bne     a0,zero,1f      # movcc
>         mv      a1,zero
> 1:
>         mv      a0,a1
>         ret
>
> after patch:
> primitiveSemantics_00:
>         czero.eqz       a0,a1,a0
>         ret
>
> Co-authored-by: Xiao Zeng<zengxiao@eswincomputing.com>
>
> gcc/ChangeLog:
>
>         * config/riscv/riscv.md (*mov<GPR:mode><X:mode>cc):move to sfb.md
>         * config/riscv/sfb.md: New file.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.target/riscv/zicond-sfb-primitiveSemantics.c: New test.
> ---
>  gcc/config/riscv/riscv.md                     | 19 +------
>  gcc/config/riscv/sfb.md                       | 37 ++++++++++++++
>  .../riscv/zicond-sfb-primitiveSemantics.c     | 50 +++++++++++++++++++
>  3 files changed, 88 insertions(+), 18 deletions(-)
>  create mode 100644 gcc/config/riscv/sfb.md
>  create mode 100644 gcc/testsuite/gcc.target/riscv/zicond-sfb-primitiveSemantics.c
>
> diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md
> index 935eeb7fd8e..d020988446f 100644
> --- a/gcc/config/riscv/riscv.md
> +++ b/gcc/config/riscv/riscv.md
> @@ -2711,24 +2711,6 @@
>    DONE;
>  })
>
> -;; Patterns for implementations that optimize short forward branches.
> -
> -(define_insn "*mov<GPR:mode><X:mode>cc"
> -  [(set (match_operand:GPR 0 "register_operand" "=r,r")
> -       (if_then_else:GPR
> -        (match_operator 5 "ordered_comparison_operator"
> -               [(match_operand:X 1 "register_operand" "r,r")
> -                (match_operand:X 2 "reg_or_0_operand" "rJ,rJ")])
> -        (match_operand:GPR 3 "register_operand" "0,0")
> -        (match_operand:GPR 4 "sfb_alu_operand" "rJ,IL")))]
> -  "TARGET_SFB_ALU"
> -  "@
> -   b%C5\t%1,%z2,1f\t# movcc\;mv\t%0,%z4\n1:
> -   b%C5\t%1,%z2,1f\t# movcc\;li\t%0,%4\n1:"
> -  [(set_attr "length" "8")
> -   (set_attr "type" "sfb_alu")
> -   (set_attr "mode" "<GPR:MODE>")])
> -
>  ;; Used to implement built-in functions.
>  (define_expand "condjump"
>    [(set (pc)
> @@ -3748,5 +3730,6 @@
>  (include "generic-ooo.md")
>  (include "vector.md")
>  (include "zicond.md")
> +(include "sfb.md")
>  (include "zc.md")
>  (include "corev.md")
> diff --git a/gcc/config/riscv/sfb.md b/gcc/config/riscv/sfb.md
> new file mode 100644
> index 00000000000..52af4b17d46
> --- /dev/null
> +++ b/gcc/config/riscv/sfb.md
> @@ -0,0 +1,37 @@
> +;; Machine description for short forward branches(SFB).
> +;; Copyright (C) 2023 Free Software Foundation, Inc.
> +
> +;; This file is part of GCC.
> +
> +;; GCC is free software; you can redistribute it and/or modify
> +;; it under the terms of the GNU General Public License as published by
> +;; the Free Software Foundation; either version 3, or (at your option)
> +;; any later version.
> +
> +;; GCC is distributed in the hope that it will be useful,
> +;; but WITHOUT ANY WARRANTY; without even the implied warranty of
> +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +;; GNU General Public License for more details.
> +
> +;; You should have received a copy of the GNU General Public License
> +;; along with GCC; see the file COPYING3.  If not see
> +;; <http://www.gnu.org/licenses/>.
> +
> +
> +;; Patterns for implementations that optimize short forward branches.
> +
> +(define_insn "*mov<GPR:mode><X:mode>cc"
> +  [(set (match_operand:GPR 0 "register_operand" "=r,r")
> +       (if_then_else:GPR
> +        (match_operator 5 "ordered_comparison_operator"
> +               [(match_operand:X 1 "register_operand" "r,r")
> +                (match_operand:X 2 "reg_or_0_operand" "rJ,rJ")])
> +        (match_operand:GPR 3 "register_operand" "0,0")
> +        (match_operand:GPR 4 "sfb_alu_operand" "rJ,IL")))]
> +  "TARGET_SFB_ALU"
> +  "@
> +   b%C5\t%1,%z2,1f\t# movcc\;mv\t%0,%z4\n1:
> +   b%C5\t%1,%z2,1f\t# movcc\;li\t%0,%4\n1:"
> +  [(set_attr "length" "8")
> +   (set_attr "type" "sfb_alu")
> +   (set_attr "mode" "<GPR:MODE>")])
> diff --git a/gcc/testsuite/gcc.target/riscv/zicond-sfb-primitiveSemantics.c b/gcc/testsuite/gcc.target/riscv/zicond-sfb-primitiveSemantics.c
> new file mode 100644
> index 00000000000..2c60656d5eb
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/riscv/zicond-sfb-primitiveSemantics.c
> @@ -0,0 +1,50 @@
> +/* { dg-do compile } */
> +/* { dg-options "-mtune=sifive-7-series -march=rv64gc_zicond -mabi=lp64d" { target { rv64 } } } */
> +/* { dg-options "-mtune=sifive-7-series -march=rv32gc_zicond -mabi=ilp32f" { target { rv32 } } } */
> +/* { dg-skip-if "" { *-*-* } {"-O0" "-Og"} } */
> +
> +long primitiveSemantics_00(long a, long b) { return a == 0 ? 0 : b; }
> +
> +long primitiveSemantics_01(long a, long b) { return a != 0 ? 0 : b; }
> +
> +long primitiveSemantics_02(long a, long b) { return a == 0 ? b : 0; }
> +
> +long primitiveSemantics_03(long a, long b) { return a != 0 ? b : 0; }
> +
> +long primitiveSemantics_04(long a, long b) {
> +  if (a)
> +    b = 0;
> +  return b;
> +}
> +
> +long primitiveSemantics_05(long a, long b) {
> +  if (!a)
> +    b = 0;
> +  return b;
> +}
> +
> +int primitiveSemantics_06(int a, int b) { return a == 0 ? 0 : b; }
> +
> +int primitiveSemantics_07(int a, int b) { return a != 0 ? 0 : b; }
> +
> +int primitiveSemantics_08(int a, int b) { return a == 0 ? b : 0; }
> +
> +int primitiveSemantics_09(int a, int b) { return a != 0 ? b : 0; }
> +
> +int primitiveSemantics_10(int a, int b) {
> +  if (a)
> +    b = 0;
> +  return b;
> +}
> +
> +int primitiveSemantics_11(int a, int b) {
> +  if (!a)
> +    b = 0;
> +  return b;
> +}
> +
> +/* { dg-final { scan-assembler-times {\mczero\.eqz\M} 6 } } */
> +/* { dg-final { scan-assembler-times {\mczero\.nez\M} 6 } } */
> +/* { dg-final { scan-assembler-not {\mbeq\M} } } */
> +/* { dg-final { scan-assembler-not {\mbne\M} } } */
> +/* { dg-final { scan-assembler-not {\mmovcc\M} } } */
> --
> 2.17.1
>
  
Jeff Law Nov. 28, 2023, 5:03 a.m. UTC | #2
On 11/27/23 20:09, Kito Cheng wrote:
> Personally I don't like to play with the pattern order to tweak the
> code gen since it kinda introduces implicit relation/rule here, but I
> guess the only way to prevent that is to duplicate the pattern for SFB
> again, which is not an ideal solution...
I won't object to this patch, but I don't really like it either.

This patch highlights that the SFB code is not well integrated with the 
rest of the conditional move support.

Jeff
  
Fei Gao Dec. 4, 2023, 7:01 a.m. UTC | #3
Committed.  Thanks Kito and Jeff.

BR
Fei

On 2023-11-28 13:03  Jeff Law <jeffreyalaw@gmail.com> wrote:
>
>
>
>On 11/27/23 20:09, Kito Cheng wrote:
>> Personally I don't like to play with the pattern order to tweak the
>> code gen since it kinda introduces implicit relation/rule here, but I
>> guess the only way to prevent that is to duplicate the pattern for SFB
>> again, which is not an ideal solution...
>I won't object to this patch, but I don't really like it either.
>
>This patch highlights that the SFB code is not well integrated with the
>rest of the conditional move support.
>
>Jeff
  

Patch

diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md
index 935eeb7fd8e..d020988446f 100644
--- a/gcc/config/riscv/riscv.md
+++ b/gcc/config/riscv/riscv.md
@@ -2711,24 +2711,6 @@ 
   DONE;
 })
 
-;; Patterns for implementations that optimize short forward branches.
-
-(define_insn "*mov<GPR:mode><X:mode>cc"
-  [(set (match_operand:GPR 0 "register_operand" "=r,r")
-	(if_then_else:GPR
-	 (match_operator 5 "ordered_comparison_operator"
-		[(match_operand:X 1 "register_operand" "r,r")
-		 (match_operand:X 2 "reg_or_0_operand" "rJ,rJ")])
-	 (match_operand:GPR 3 "register_operand" "0,0")
-	 (match_operand:GPR 4 "sfb_alu_operand" "rJ,IL")))]
-  "TARGET_SFB_ALU"
-  "@
-   b%C5\t%1,%z2,1f\t# movcc\;mv\t%0,%z4\n1:
-   b%C5\t%1,%z2,1f\t# movcc\;li\t%0,%4\n1:"
-  [(set_attr "length" "8")
-   (set_attr "type" "sfb_alu")
-   (set_attr "mode" "<GPR:MODE>")])
-
 ;; Used to implement built-in functions.
 (define_expand "condjump"
   [(set (pc)
@@ -3748,5 +3730,6 @@ 
 (include "generic-ooo.md")
 (include "vector.md")
 (include "zicond.md")
+(include "sfb.md")
 (include "zc.md")
 (include "corev.md")
diff --git a/gcc/config/riscv/sfb.md b/gcc/config/riscv/sfb.md
new file mode 100644
index 00000000000..52af4b17d46
--- /dev/null
+++ b/gcc/config/riscv/sfb.md
@@ -0,0 +1,37 @@ 
+;; Machine description for short forward branches(SFB).
+;; Copyright (C) 2023 Free Software Foundation, Inc.
+
+;; This file is part of GCC.
+
+;; GCC is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 3, or (at your option)
+;; any later version.
+
+;; GCC is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GCC; see the file COPYING3.  If not see
+;; <http://www.gnu.org/licenses/>.
+
+
+;; Patterns for implementations that optimize short forward branches.
+
+(define_insn "*mov<GPR:mode><X:mode>cc"
+  [(set (match_operand:GPR 0 "register_operand" "=r,r")
+	(if_then_else:GPR
+	 (match_operator 5 "ordered_comparison_operator"
+		[(match_operand:X 1 "register_operand" "r,r")
+		 (match_operand:X 2 "reg_or_0_operand" "rJ,rJ")])
+	 (match_operand:GPR 3 "register_operand" "0,0")
+	 (match_operand:GPR 4 "sfb_alu_operand" "rJ,IL")))]
+  "TARGET_SFB_ALU"
+  "@
+   b%C5\t%1,%z2,1f\t# movcc\;mv\t%0,%z4\n1:
+   b%C5\t%1,%z2,1f\t# movcc\;li\t%0,%4\n1:"
+  [(set_attr "length" "8")
+   (set_attr "type" "sfb_alu")
+   (set_attr "mode" "<GPR:MODE>")])
diff --git a/gcc/testsuite/gcc.target/riscv/zicond-sfb-primitiveSemantics.c b/gcc/testsuite/gcc.target/riscv/zicond-sfb-primitiveSemantics.c
new file mode 100644
index 00000000000..2c60656d5eb
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zicond-sfb-primitiveSemantics.c
@@ -0,0 +1,50 @@ 
+/* { dg-do compile } */
+/* { dg-options "-mtune=sifive-7-series -march=rv64gc_zicond -mabi=lp64d" { target { rv64 } } } */
+/* { dg-options "-mtune=sifive-7-series -march=rv32gc_zicond -mabi=ilp32f" { target { rv32 } } } */
+/* { dg-skip-if "" { *-*-* } {"-O0" "-Og"} } */
+
+long primitiveSemantics_00(long a, long b) { return a == 0 ? 0 : b; }
+
+long primitiveSemantics_01(long a, long b) { return a != 0 ? 0 : b; }
+
+long primitiveSemantics_02(long a, long b) { return a == 0 ? b : 0; }
+
+long primitiveSemantics_03(long a, long b) { return a != 0 ? b : 0; }
+
+long primitiveSemantics_04(long a, long b) {
+  if (a)
+    b = 0;
+  return b;
+}
+
+long primitiveSemantics_05(long a, long b) {
+  if (!a)
+    b = 0;
+  return b;
+}
+
+int primitiveSemantics_06(int a, int b) { return a == 0 ? 0 : b; }
+
+int primitiveSemantics_07(int a, int b) { return a != 0 ? 0 : b; }
+
+int primitiveSemantics_08(int a, int b) { return a == 0 ? b : 0; }
+
+int primitiveSemantics_09(int a, int b) { return a != 0 ? b : 0; }
+
+int primitiveSemantics_10(int a, int b) {
+  if (a)
+    b = 0;
+  return b;
+}
+
+int primitiveSemantics_11(int a, int b) {
+  if (!a)
+    b = 0;
+  return b;
+}
+
+/* { dg-final { scan-assembler-times {\mczero\.eqz\M} 6 } } */
+/* { dg-final { scan-assembler-times {\mczero\.nez\M} 6 } } */
+/* { dg-final { scan-assembler-not {\mbeq\M} } } */
+/* { dg-final { scan-assembler-not {\mbne\M} } } */
+/* { dg-final { scan-assembler-not {\mmovcc\M} } } */