]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
RISC-V: Remove masking third operand of rotate instructions
authorJivan Hakobyan <jivanhakobyan9@gmail.com>
Wed, 17 May 2023 19:00:28 +0000 (13:00 -0600)
committerJeff Law <jlaw@ventanamicro.com>
Wed, 17 May 2023 19:04:35 +0000 (13:04 -0600)
    Rotate instructions do not need to mask the third operand.
    For example,  RV64 the following code:

    unsigned long foo1(unsigned long rs1, unsigned long rs2)
    {
        long shamt = rs2 & (64 - 1);
        return (rs1 << shamt) | (rs1 >> ((64 - shamt) & (64 - 1)));
    }

    Compiles to:
    foo1:
            andi    a1,a1,63
            rol     a0,a0,a1
            ret

    This patch removes unnecessary masking.
    Besides, I have merged masking insns for shifts that were written before.

gcc/ChangeLog:
* config/riscv/riscv.md (*<optab><GPR:mode>3_mask): New pattern,
combined from ...
(*<optab>si3_mask, *<optab>di3_mask): Here.
(*<optab>si3_mask_1, *<optab>di3_mask_1): And here.
* config/riscv/bitmanip.md (*<bitmanip_optab><GPR:mode>3_mask): New
pattern.
(*<bitmanip_optab>si3_sext_mask): Likewise.
* config/riscv/iterators.md (shiftm1): Use const_si_mask_operand
and const_di_mask_operand.
(bitmanip_rotate): New iterator.
(bitmanip_optab): Add rotates.
* config/riscv/predicates.md (const_si_mask_operand): Renamed
from const31_operand.  Generalize to handle more mask constants.
(const_di_mask_operand): Similarly.

gcc/testsuite/ChangeLog:
* gcc.target/riscv/shift-and-2.c: Fixed test
* gcc.target/riscv/zbb-rol-ror-01.c: New test
* gcc.target/riscv/zbb-rol-ror-02.c: New test
* gcc.target/riscv/zbb-rol-ror-03.c: New test
* gcc.target/riscv/zbb-rol-ror-04.c: New test
* gcc.target/riscv/zbb-rol-ror-05.c: New test
* gcc.target/riscv/zbb-rol-ror-06.c: New test
* gcc.target/riscv/zbb-rol-ror-07.c: New test

12 files changed:
gcc/config/riscv/bitmanip.md
gcc/config/riscv/iterators.md
gcc/config/riscv/predicates.md
gcc/config/riscv/riscv.md
gcc/testsuite/gcc.target/riscv/shift-and-2.c
gcc/testsuite/gcc.target/riscv/zbb-rol-ror-01.c
gcc/testsuite/gcc.target/riscv/zbb-rol-ror-02.c
gcc/testsuite/gcc.target/riscv/zbb-rol-ror-03.c
gcc/testsuite/gcc.target/riscv/zbb-rol-ror-04.c
gcc/testsuite/gcc.target/riscv/zbb-rol-ror-05.c
gcc/testsuite/gcc.target/riscv/zbb-rol-ror-06.c
gcc/testsuite/gcc.target/riscv/zbb-rol-ror-07.c

index a27fc3e34a1723f78b93f998c4a7f91751fa2913..6f3d24a8a88a79b545362951a5a7e7efa9de8dd3 100644 (file)
   "rolw\t%0,%1,%2"
   [(set_attr "type" "bitmanip")])
 
+(define_insn_and_split "*<bitmanip_optab><GPR:mode>3_mask"
+  [(set (match_operand:GPR     0 "register_operand" "= r")
+        (bitmanip_rotate:GPR
+            (match_operand:GPR 1 "register_operand" "  r")
+            (match_operator 4 "subreg_lowpart_operator"
+             [(and:GPR2
+               (match_operand:GPR2 2 "register_operand"  "r")
+               (match_operand 3 "<GPR:shiftm1>" "<GPR:shiftm1p>"))])))]
+  "TARGET_ZBB || TARGET_ZBKB"
+  "#"
+  "&& 1"
+  [(set (match_dup 0)
+        (bitmanip_rotate:GPR (match_dup 1)
+                             (match_dup 2)))]
+  "operands[2] = gen_lowpart (QImode, operands[2]);"
+  [(set_attr "type" "bitmanip")
+   (set_attr "mode" "<GPR:MODE>")])
+
+(define_insn_and_split "*<bitmanip_optab>si3_sext_mask"
+  [(set (match_operand:DI     0 "register_operand" "= r")
+  (sign_extend:DI (bitmanip_rotate:SI
+            (match_operand:SI 1 "register_operand" "  r")
+            (match_operator 4 "subreg_lowpart_operator"
+             [(and:GPR
+               (match_operand:GPR 2 "register_operand"  "r")
+               (match_operand 3 "const_si_mask_operand"))]))))]
+  "TARGET_64BIT && (TARGET_ZBB || TARGET_ZBKB)"
+  "#"
+  "&& 1"
+  [(set (match_dup 0)
+  (sign_extend:DI (bitmanip_rotate:SI (match_dup 1)
+                           (match_dup 2))))]
+  "operands[2] = gen_lowpart (QImode, operands[2]);"
+  [(set_attr "type" "bitmanip")
+   (set_attr "mode" "DI")])
+
 ;; orc.b (or-combine) is added as an unspec for the benefit of the support
 ;; for optimized string functions (such as strcmp).
 (define_insn "orcb<mode>2"
index 1d56324df03afb9e34e98e23aa448867dc953570..8afe98e44109cbeddc717f7523f67368cd01bb61 100644 (file)
 (define_mode_attr HALFMODE [(DF "SI") (DI "SI") (TF "DI")])
 
 ; bitmanip mode attribute
-(define_mode_attr shiftm1 [(SI "const31_operand") (DI "const63_operand")])
+(define_mode_attr shiftm1 [(SI "const_si_mask_operand") (DI "const_di_mask_operand")])
 (define_mode_attr shiftm1p [(SI "DsS") (DI "DsD")])
 
 ;; -------------------------------------------------------------------
 
 (define_code_iterator clz_ctz_pcnt [clz ctz popcount])
 
+(define_code_iterator bitmanip_rotate [rotate rotatert])
+
 ;; -------------------------------------------------------------------
 ;; Code Attributes
 ;; -------------------------------------------------------------------
                                  (umax "umax")
                                  (clz "clz")
                                  (ctz "ctz")
-                                 (popcount "popcount")])
+                                 (popcount "popcount")
+                                 (rotate "rotl")
+                                 (rotatert "rotr")])
 (define_code_attr bitmanip_insn [(smin "min")
                                 (smax "max")
                                 (umin "minu")
index e5adf06fa254af39f0ded5d1a97b4975a934864e..ffcbb9a75899730033112159d759b00fb1464d2d 100644 (file)
   (and (match_code "const_int")
        (match_test "SINGLE_BIT_MASK_OPERAND (~UINTVAL (op))")))
 
-(define_predicate "const31_operand"
+(define_predicate "const_si_mask_operand"
   (and (match_code "const_int")
-       (match_test "INTVAL (op) == 31")))
+       (match_test "(INTVAL (op) & (GET_MODE_BITSIZE (SImode) - 1))
+                    == GET_MODE_BITSIZE (SImode) - 1")))
 
-(define_predicate "const63_operand"
+(define_predicate "const_di_mask_operand"
   (and (match_code "const_int")
-       (match_test "INTVAL (op) == 63")))
+       (match_test "(INTVAL (op) & (GET_MODE_BITSIZE (DImode) - 1))
+                    == GET_MODE_BITSIZE (DImode) - 1")))
 
 (define_predicate "imm5_operand"
   (and (match_code "const_int")
index a917993121786f1dfe8332896672900a538d4478..e773bc748bf00302c9f7e32fb6b5006a7fc8db63 100644 (file)
   [(set_attr "type" "shift")
    (set_attr "mode" "SI")])
 
-(define_insn_and_split "*<optab>si3_mask"
-  [(set (match_operand:SI     0 "register_operand" "= r")
-       (any_shift:SI
-           (match_operand:SI 1 "register_operand" "  r")
-           (match_operator 4 "subreg_lowpart_operator"
-            [(and:SI
-              (match_operand:SI 2 "register_operand"  "r")
-              (match_operand 3 "const_int_operand"))])))]
-  "(INTVAL (operands[3]) & (GET_MODE_BITSIZE (SImode)-1))
-   == GET_MODE_BITSIZE (SImode)-1"
-  "#"
-  "&& 1"
-  [(set (match_dup 0)
-       (any_shift:SI (match_dup 1)
-                     (match_dup 2)))]
-  "operands[2] = gen_lowpart (QImode, operands[2]);"
-  [(set_attr "type" "shift")
-   (set_attr "mode" "SI")])
-
-(define_insn_and_split "*<optab>si3_mask_1"
-  [(set (match_operand:SI     0 "register_operand" "= r")
-       (any_shift:SI
-           (match_operand:SI 1 "register_operand" "  r")
-           (match_operator 4 "subreg_lowpart_operator"
-            [(and:DI
-              (match_operand:DI 2 "register_operand"  "r")
-              (match_operand 3 "const_int_operand"))])))]
-  "TARGET_64BIT
-   && (INTVAL (operands[3]) & (GET_MODE_BITSIZE (SImode)-1))
-       == GET_MODE_BITSIZE (SImode)-1"
-  "#"
-  "&& 1"
-  [(set (match_dup 0)
-       (any_shift:SI (match_dup 1)
-                     (match_dup 2)))]
-  "operands[2] = gen_lowpart (QImode, operands[2]);"
-  [(set_attr "type" "shift")
-   (set_attr "mode" "SI")])
-
 (define_insn "<optab>di3"
   [(set (match_operand:DI 0 "register_operand"     "= r")
        (any_shift:DI
   [(set_attr "type" "shift")
    (set_attr "mode" "DI")])
 
-(define_insn_and_split "*<optab>di3_mask"
-  [(set (match_operand:DI     0 "register_operand" "= r")
-       (any_shift:DI
-           (match_operand:DI 1 "register_operand" "  r")
+(define_insn_and_split "*<optab><GPR:mode>3_mask_1"
+  [(set (match_operand:GPR     0 "register_operand" "= r")
+       (any_shift:GPR
+           (match_operand:GPR 1 "register_operand" "  r")
            (match_operator 4 "subreg_lowpart_operator"
-            [(and:SI
-              (match_operand:SI 2 "register_operand"  "r")
-              (match_operand 3 "const_int_operand"))])))]
-  "TARGET_64BIT
-   && (INTVAL (operands[3]) & (GET_MODE_BITSIZE (DImode)-1))
-       == GET_MODE_BITSIZE (DImode)-1"
-  "#"
-  "&& 1"
-  [(set (match_dup 0)
-       (any_shift:DI (match_dup 1)
-                     (match_dup 2)))]
-  "operands[2] = gen_lowpart (QImode, operands[2]);"
-  [(set_attr "type" "shift")
-   (set_attr "mode" "DI")])
-
-(define_insn_and_split "*<optab>di3_mask_1"
-  [(set (match_operand:DI     0 "register_operand" "= r")
-       (any_shift:DI
-           (match_operand:DI 1 "register_operand" "  r")
-           (match_operator 4 "subreg_lowpart_operator"
-            [(and:DI
-              (match_operand:DI 2 "register_operand"  "r")
-              (match_operand 3 "const_int_operand"))])))]
-  "TARGET_64BIT
-   && (INTVAL (operands[3]) & (GET_MODE_BITSIZE (DImode)-1))
-       == GET_MODE_BITSIZE (DImode)-1"
+            [(and:GPR2
+              (match_operand:GPR2 2 "register_operand"  "r")
+              (match_operand 3 "<GPR:shiftm1>"))])))]
+  ""
   "#"
   "&& 1"
   [(set (match_dup 0)
-       (any_shift:DI (match_dup 1)
+       (any_shift:GPR (match_dup 1)
                      (match_dup 2)))]
   "operands[2] = gen_lowpart (QImode, operands[2]);"
   [(set_attr "type" "shift")
-   (set_attr "mode" "DI")])
+   (set_attr "mode" "<GPR:MODE>")])
 
 (define_insn "*<optab>si3_extend"
   [(set (match_operand:DI                   0 "register_operand" "= r")
            (any_shift:SI
             (match_operand:SI 1 "register_operand" "  r")
             (match_operator 4 "subreg_lowpart_operator"
-             [(and:SI
-               (match_operand:SI 2 "register_operand" " r")
-               (match_operand 3 "const_int_operand"))]))))]
-  "TARGET_64BIT
-   && (INTVAL (operands[3]) & (GET_MODE_BITSIZE (SImode)-1))
-       == GET_MODE_BITSIZE (SImode)-1"
-  "#"
-  "&& 1"
-  [(set (match_dup 0)
-       (sign_extend:DI
-        (any_shift:SI (match_dup 1)
-                      (match_dup 2))))]
-  "operands[2] = gen_lowpart (QImode, operands[2]);"
-  [(set_attr "type" "shift")
-   (set_attr "mode" "SI")])
-
-(define_insn_and_split "*<optab>si3_extend_mask_1"
-  [(set (match_operand:DI                   0 "register_operand" "= r")
-       (sign_extend:DI
-           (any_shift:SI
-            (match_operand:SI 1 "register_operand" "  r")
-            (match_operator 4 "subreg_lowpart_operator"
-             [(and:DI
-               (match_operand:DI 2 "register_operand" " r")
-               (match_operand 3 "const_int_operand"))]))))]
-  "TARGET_64BIT
-   && (INTVAL (operands[3]) & (GET_MODE_BITSIZE (SImode)-1))
-       == GET_MODE_BITSIZE (SImode)-1"
+             [(and:GPR
+               (match_operand:GPR 2 "register_operand" " r")
+               (match_operand 3 "const_si_mask_operand"))]))))]
+  "TARGET_64BIT"
   "#"
   "&& 1"
   [(set (match_dup 0)
index 360d8417209a933cd75d1ccb43a34853f355eb56..bc01e8ef99268ff154f0759620d501cadf338561 100644 (file)
@@ -11,10 +11,10 @@ sub2 (int i, long j)
 }
 
 /* Test for <optab>si3_extend_mask.  */
-unsigned long
-sub3 (int mask)
+int
+sub3 (short mask)
 {
-  return 1 << (mask & 0xff);
+  return 1 << ((int)mask & 0x1f);
 }
 
 /* Test for <optab>si3_extend_mask_1.  */
index 20c1b2856ef114254f78ef6b02f4551178efe54e..0a5b5e12eb2a6f4d5fd8b800231c2261c2ede542 100644 (file)
@@ -14,4 +14,5 @@ unsigned long foo2(unsigned long rs1, unsigned long rs2)
 }
 
 /* { dg-final { scan-assembler-times "rol" 2 } } */
-/* { dg-final { scan-assembler-times "ror" 2 } } */
\ No newline at end of file
+/* { dg-final { scan-assembler-times "ror" 2 } } */
+/* { dg-final { scan-assembler-not "and" } } */
\ No newline at end of file
index 14196c11fb989bb6f5e2efce355447a91c016442..d0d5813580922ca45b8dab28a4ce4f200bfd3320 100644 (file)
@@ -14,4 +14,5 @@ unsigned int foo2(unsigned int rs1, unsigned int rs2)
 }
 
 /* { dg-final { scan-assembler-times "rol" 2 } } */
-/* { dg-final { scan-assembler-times "ror" 2 } } */
\ No newline at end of file
+/* { dg-final { scan-assembler-times "ror" 2 } } */
+/* { dg-final { scan-assembler-not {and} { target { no-opts "-O0" } } } } */
\ No newline at end of file
index ed4685dc7ac84d4674b1a33779ab0e14639774b7..b44d7fe8920b3a6bdb81329626f20a508d0ca277 100644 (file)
@@ -15,4 +15,5 @@ unsigned int ror(unsigned int rs1, unsigned int rs2)
 }
 
 /* { dg-final { scan-assembler-times "rolw" 1 } } */
-/* { dg-final { scan-assembler-times "rorw" 1 } } */
\ No newline at end of file
+/* { dg-final { scan-assembler-times "rorw" 1 } } */
+/* { dg-final { scan-assembler-not "and" } } */
\ No newline at end of file
index 08053484cb2427cad04da7bdb7c12d8793e849bd..7ef4c29dd5b5a74fc2ac6db5859fc205cbcf479f 100644 (file)
@@ -2,6 +2,7 @@
 /* { dg-options "-march=rv64gc_zbb -mabi=lp64d -fno-lto -O2" } */
 /* { dg-skip-if "" { *-*-* } { "-g" } } */
 /* { dg-final { check-function-bodies "**" "" } } */
+/* { dg-final { scan-assembler-not "and" } } */
 
 /*
 **foo1:
index 85090b1b0fc90602b58eeea95b1a63666fe1fb7d..2108ccc3e779da380b5b6ddc72d58285c49b1844 100644 (file)
@@ -2,6 +2,7 @@
 /* { dg-options "-march=rv32gc_zbb -mabi=ilp32 -fno-lto -O2" } */
 /* { dg-skip-if "" { *-*-* } { "-g" } } */
 /* { dg-final { check-function-bodies "**" "" } } */
+/* { dg-final { scan-assembler-not "and" } } */
 
 /*
 **foo1:
index 70b79abb6ed0d0db2e72a26264d5e6985b0758e5..8c0711d6f9481ec40bc0f385e4a8cf8e3b2dbde7 100644 (file)
@@ -2,6 +2,7 @@
 /* { dg-options "-march=rv64gc_zbb -mabi=lp64d -fno-lto -O2" } */
 /* { dg-skip-if "" { *-*-* } { "-g" } } */
 /* { dg-final { check-function-bodies "**" "" } } */
+/* { dg-final { scan-assembler-not "and" } } */
 
 /*
 **foo1:
index 3b6ab385a857d7cfe42078c7b06a980beab67f96..bda3f0e474d5b51d11136b60db7205568510e66c 100644 (file)
@@ -2,6 +2,7 @@
 /* { dg-options "-march=rv64gc_zbb -mabi=lp64d -fno-lto -O2" } */
 /* { dg-skip-if "" { *-*-* } { "-g" } } */
 /* { dg-final { check-function-bodies "**" "" } } */
+/* { dg-final { scan-assembler-not "and" } } */
 
 /*
 **foo1: