[06/12] RISC-V: memmove() size optimized version: Relax RAW dependency

Message ID 20250409064701.3435-7-marlene.fally@gmail.com
State New
Headers
Series newlib: RISC-V: Optimize memory and string functions for code size |

Commit Message

m fally April 9, 2025, 6:46 a.m. UTC
  Move the instruction that increments the remaining number of
bytes to be copied inbetween the load and store instructions.
This is done in order to relax the RAW dependency between the
load and store instructions.

Reviewed-by: Christian Herber <christian.herber@oss.nxp.com>
Signed-off-by: m fally <marlene.fally@gmail.com>
---
 newlib/libc/machine/riscv/memmove.S | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
  

Comments

Kito Cheng April 9, 2025, 7:46 a.m. UTC | #1
LGTM

On Wed, Apr 9, 2025 at 2:57 PM m fally <marlene.fally@gmail.com> wrote:
>
> Move the instruction that increments the remaining number of
> bytes to be copied inbetween the load and store instructions.
> This is done in order to relax the RAW dependency between the
> load and store instructions.
>
> Reviewed-by: Christian Herber <christian.herber@oss.nxp.com>
> Signed-off-by: m fally <marlene.fally@gmail.com>
> ---
>  newlib/libc/machine/riscv/memmove.S | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/newlib/libc/machine/riscv/memmove.S b/newlib/libc/machine/riscv/memmove.S
> index 671a6a158..6ecad9ad6 100644
> --- a/newlib/libc/machine/riscv/memmove.S
> +++ b/newlib/libc/machine/riscv/memmove.S
> @@ -28,9 +28,9 @@ memmove:
>    add   a4, a4, a3             /* in case of source address < destination address, increment both addresses by -1 before copying any data to obtain the correct start addresses */
>    add   a1, a1, a3
>  .Lcopy:
> -  lbu a5, 0(a1)                        /* copy bytes as long as a2 (= the number of bytes to be copied) > 0 */
> +  lbu a5, 0(a1)
> +  add   a2, a2, -1             /* copy bytes as long as a2 (= the number of bytes to be copied) > 0. the increment is done here to relax the RAW dependency between load and store */
>    sb a5, 0(a4)
> -  add   a2, a2, -1
>    bnez a2, .Lincrement
>
>  .Ldone:
> --
> 2.49.0
>
  

Patch

diff --git a/newlib/libc/machine/riscv/memmove.S b/newlib/libc/machine/riscv/memmove.S
index 671a6a158..6ecad9ad6 100644
--- a/newlib/libc/machine/riscv/memmove.S
+++ b/newlib/libc/machine/riscv/memmove.S
@@ -28,9 +28,9 @@  memmove:
   add   a4, a4, a3 		/* in case of source address < destination address, increment both addresses by -1 before copying any data to obtain the correct start addresses */
   add   a1, a1, a3
 .Lcopy:
-  lbu a5, 0(a1)			/* copy bytes as long as a2 (= the number of bytes to be copied) > 0 */
+  lbu a5, 0(a1)
+  add   a2, a2, -1		/* copy bytes as long as a2 (= the number of bytes to be copied) > 0. the increment is done here to relax the RAW dependency between load and store */
   sb a5, 0(a4)
-  add   a2, a2, -1
   bnez a2, .Lincrement
 
 .Ldone: