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 <gao...@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<zengx...@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
>

Reply via email to