]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
RISC-V: Rename and unify stringop strategy handling.
authorRobin Dapp <rdapp@ventanamicro.com>
Fri, 1 Dec 2023 08:30:17 +0000 (09:30 +0100)
committerRobin Dapp <rdapp@ventanamicro.com>
Mon, 4 Dec 2023 15:23:56 +0000 (16:23 +0100)
In preparation for the vectorized strlen and strcmp support this NFC
patch unifies the stringop strategy handling a bit.  The "auto"
strategy now is a combination of scalar and vector and an expander
should try the strategies in their preferred order.

For the block_move expander this patch does just that.

gcc/ChangeLog:

* config/riscv/riscv-opts.h (enum riscv_stringop_strategy_enum):
Rename...
(enum stringop_strategy_enum): ... to this.
* config/riscv/riscv-string.cc (riscv_expand_block_move): New
wrapper expander handling the strategies and delegation.
(riscv_expand_block_move_scalar): Rename function and make
static.
(expand_block_move): Remove strategy handling.
* config/riscv/riscv.md: Call expander wrapper.
* config/riscv/riscv.opt: Rename.

gcc/testsuite/ChangeLog:

* gcc.target/riscv/rvv/base/cpymem-strategy-1.c: Change to
-mstringop-strategy.
* gcc.target/riscv/rvv/base/cpymem-strategy-2.c: Ditto.
* gcc.target/riscv/rvv/base/cpymem-strategy-3.c: Ditto.
* gcc.target/riscv/rvv/base/cpymem-strategy-4.c: Ditto.
* gcc.target/riscv/rvv/base/cpymem-strategy-5.c: Ditto.

gcc/config/riscv/riscv-opts.h
gcc/config/riscv/riscv-string.cc
gcc/config/riscv/riscv.md
gcc/config/riscv/riscv.opt
gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-1.c
gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-2.c
gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-3.c
gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-4.c
gcc/testsuite/gcc.target/riscv/rvv/base/cpymem-strategy-5.c

index e6e55ad70719631b50baed7a279a1185979c1ecb..30efebbf07b0d9e11c4f9f7b3af8ded0bd18ed27 100644 (file)
@@ -104,15 +104,15 @@ enum riscv_entity
 };
 
 /* RISC-V stringop strategy. */
-enum riscv_stringop_strategy_enum {
-  /* Use scalar or vector instructions. */
-  USE_AUTO,
-  /* Always use a library call. */
-  USE_LIBCALL,
-  /* Only use scalar instructions. */
-  USE_SCALAR,
-  /* Only use vector instructions. */
-  USE_VECTOR
+enum stringop_strategy_enum {
+  /* No expansion. */
+  STRATEGY_LIBCALL = 1,
+  /* Use scalar expansion if possible. */
+  STRATEGY_SCALAR = 2,
+  /* Only vector expansion if possible. */
+  STRATEGY_VECTOR = 4,
+  /* Use any. */
+  STRATEGY_AUTO = STRATEGY_SCALAR | STRATEGY_VECTOR
 };
 
 #define TARGET_ZICOND_LIKE (TARGET_ZICOND || (TARGET_XVENTANACONDOPS && TARGET_64BIT))
index 80e3b5981af73a056ed81558b961a01bed9fa226..f3a4d3ddd47f67a76f9cf07313ad1e6bd3407be7 100644 (file)
@@ -707,51 +707,68 @@ riscv_block_move_loop (rtx dest, rtx src, unsigned HOST_WIDE_INT length,
 /* Expand a cpymemsi instruction, which copies LENGTH bytes from
    memory reference SRC to memory reference DEST.  */
 
-bool
-riscv_expand_block_move (rtx dest, rtx src, rtx length)
+static bool
+riscv_expand_block_move_scalar (rtx dest, rtx src, rtx length)
 {
-  if (riscv_memcpy_strategy == USE_LIBCALL
-      || riscv_memcpy_strategy == USE_VECTOR)
+  if (!CONST_INT_P (length))
     return false;
 
-  if (CONST_INT_P (length))
-    {
-      unsigned HOST_WIDE_INT hwi_length = UINTVAL (length);
-      unsigned HOST_WIDE_INT factor, align;
+  unsigned HOST_WIDE_INT hwi_length = UINTVAL (length);
+  unsigned HOST_WIDE_INT factor, align;
 
-      align = MIN (MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), BITS_PER_WORD);
-      factor = BITS_PER_WORD / align;
+  align = MIN (MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), BITS_PER_WORD);
+  factor = BITS_PER_WORD / align;
 
-      if (optimize_function_for_size_p (cfun)
-         && hwi_length * factor * UNITS_PER_WORD > MOVE_RATIO (false))
-       return false;
+  if (optimize_function_for_size_p (cfun)
+      && hwi_length * factor * UNITS_PER_WORD > MOVE_RATIO (false))
+    return false;
 
-      if (hwi_length <= (RISCV_MAX_MOVE_BYTES_STRAIGHT / factor))
+  if (hwi_length <= (RISCV_MAX_MOVE_BYTES_STRAIGHT / factor))
+    {
+      riscv_block_move_straight (dest, src, INTVAL (length));
+      return true;
+    }
+  else if (optimize && align >= BITS_PER_WORD)
+    {
+      unsigned min_iter_words
+       = RISCV_MAX_MOVE_BYTES_PER_LOOP_ITER / UNITS_PER_WORD;
+      unsigned iter_words = min_iter_words;
+      unsigned HOST_WIDE_INT bytes = hwi_length;
+      unsigned HOST_WIDE_INT words = bytes / UNITS_PER_WORD;
+
+      /* Lengthen the loop body if it shortens the tail.  */
+      for (unsigned i = min_iter_words; i < min_iter_words * 2 - 1; i++)
        {
-         riscv_block_move_straight (dest, src, INTVAL (length));
-         return true;
+         unsigned cur_cost = iter_words + words % iter_words;
+         unsigned new_cost = i + words % i;
+         if (new_cost <= cur_cost)
+           iter_words = i;
        }
-      else if (optimize && align >= BITS_PER_WORD)
-       {
-         unsigned min_iter_words
-           = RISCV_MAX_MOVE_BYTES_PER_LOOP_ITER / UNITS_PER_WORD;
-         unsigned iter_words = min_iter_words;
-         unsigned HOST_WIDE_INT bytes = hwi_length;
-         unsigned HOST_WIDE_INT words = bytes / UNITS_PER_WORD;
-
-         /* Lengthen the loop body if it shortens the tail.  */
-         for (unsigned i = min_iter_words; i < min_iter_words * 2 - 1; i++)
-           {
-             unsigned cur_cost = iter_words + words % iter_words;
-             unsigned new_cost = i + words % i;
-             if (new_cost <= cur_cost)
-               iter_words = i;
-           }
 
-         riscv_block_move_loop (dest, src, bytes, iter_words * UNITS_PER_WORD);
-         return true;
-       }
+      riscv_block_move_loop (dest, src, bytes, iter_words * UNITS_PER_WORD);
+      return true;
+    }
+
+  return false;
+}
+
+/* This function delegates block-move expansion to either the vector
+   implementation or the scalar one.  Return TRUE if successful or FALSE
+   otherwise.  */
+
+bool
+riscv_expand_block_move (rtx dest, rtx src, rtx length)
+{
+  if (TARGET_VECTOR && stringop_strategy & STRATEGY_VECTOR)
+    {
+      bool ok = riscv_vector::expand_block_move (dest, src, length);
+      if (ok)
+       return true;
     }
+
+  if (stringop_strategy & STRATEGY_SCALAR)
+    return riscv_expand_block_move_scalar (dest, src, length);
+
   return false;
 }
 
@@ -777,9 +794,8 @@ expand_block_move (rtx dst_in, rtx src_in, rtx length_in)
        bnez a2, loop                   # Any more?
        ret                             # Return
   */
-  if (!TARGET_VECTOR || riscv_memcpy_strategy == USE_LIBCALL
-      || riscv_memcpy_strategy == USE_SCALAR)
-    return false;
+  gcc_assert (TARGET_VECTOR);
+
   HOST_WIDE_INT potential_ew
     = (MIN (MIN (MEM_ALIGN (src_in), MEM_ALIGN (dst_in)), BITS_PER_WORD)
        / BITS_PER_UNIT);
index 96abdfcb86ff33c4b2b3fc4563d6d0665c7d0dc9..a98918dfd434aae8aa830691e0f0c60a76b4b100 100644 (file)
              (use (match_operand:SI 3 "const_int_operand"))])]
   ""
 {
-  if (riscv_vector::expand_block_move (operands[0], operands[1], operands[2]))
-    DONE;
-  else if (riscv_expand_block_move (operands[0], operands[1], operands[2]))
+  if (riscv_expand_block_move (operands[0], operands[1], operands[2]))
     DONE;
   else
     FAIL;
index 78186fff6c5ddbe08cc40fb539700d46e7e20e7d..59ce7106ecfa5b312afdd5eb0ae02ba15748341d 100644 (file)
@@ -538,21 +538,21 @@ Enable the use of vector registers for function arguments and return value.
 This is an experimental switch and may be subject to change in the future.
 
 Enum
-Name(riscv_stringop_strategy) Type(enum riscv_stringop_strategy_enum)
-Valid arguments to -mmemcpy-strategy=:
+Name(stringop_strategy) Type(enum stringop_strategy_enum)
+Valid arguments to -mstringop-strategy=:
 
 EnumValue
-Enum(riscv_stringop_strategy) String(auto) Value(USE_AUTO)
+Enum(stringop_strategy) String(auto) Value(STRATEGY_AUTO)
 
 EnumValue
-Enum(riscv_stringop_strategy) String(libcall) Value(USE_LIBCALL)
+Enum(stringop_strategy) String(libcall) Value(STRATEGY_LIBCALL)
 
 EnumValue
-Enum(riscv_stringop_strategy) String(scalar) Value(USE_SCALAR)
+Enum(stringop_strategy) String(scalar) Value(STRATEGY_SCALAR)
 
 EnumValue
-Enum(riscv_stringop_strategy) String(vector) Value(USE_VECTOR)
+Enum(stringop_strategy) String(vector) Value(STRATEGY_VECTOR)
 
-mmemcpy-strategy=
-Target RejectNegative Joined Enum(riscv_stringop_strategy) Var(riscv_memcpy_strategy) Init(USE_AUTO)
-Specify memcpy expansion strategy.
+mstringop-strategy=
+Target RejectNegative Joined Enum(stringop_strategy) Var(stringop_strategy) Init(STRATEGY_AUTO)
+Specify stringop expansion strategy.
index ae49706dca561b892a3307ef9bb9f9def1d7ff97..adad5abcef200e040fb94c57a4ef93c19e3d1674 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-march=rv32gcv -mabi=ilp32d -mmemcpy-strategy=libcall" } */
+/* { dg-options "-march=rv32gcv -mabi=ilp32d -mstringop-strategy=libcall" } */
 
 #include "cpymem-strategy.h"
 
index 73ffc5783d06ce44f82bb0a32f4956595fe3c4ec..7a7c97d560cc7dc9c546ba23f5423a9c7c4a7ff7 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-O2 -march=rv32gcv -mabi=ilp32d -mmemcpy-strategy=scalar" } */
+/* { dg-options "-O2 -march=rv32gcv -mabi=ilp32d -mstringop-strategy=scalar" } */
 
 #include "cpymem-strategy.h"
 
index 44f5f7839629bf967f11641f0e24a2b768f9fd48..83e5a8377306021a15fce85167422d77907b2fca 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-march=rv32gcv -mabi=ilp32d -mmemcpy-strategy=vector" } */
+/* { dg-options "-march=rv32gcv -mabi=ilp32d -mstringop-strategy=vector" } */
 
 #include "cpymem-strategy.h"
 
index 8056895334a018d278fd521530bb33a3b79a8d33..800549c8556c85b538ad252f3dcb944fbb406201 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-march=rv32gcv -mabi=ilp32d -mmemcpy-strategy=auto" } */
+/* { dg-options "-march=rv32gcv -mabi=ilp32d -mstringop-strategy=auto" } */
 
 #include "cpymem-strategy.h"
 
index 82ecab04a403a34f1d89c9a3d987295332d455f4..134fd2e9e9b8a1436f4003c490237d2910522fa4 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-march=rv32gc -mabi=ilp32d -mmemcpy-strategy=vector" } */
+/* { dg-options "-march=rv32gc -mabi=ilp32d -mstringop-strategy=vector" } */
 
 #include "cpymem-strategy.h"