588e70
commit 10624a97e8e47004985740cbb04060a84cfada76
588e70
Author: Matheus Castanho <msc@linux.ibm.com>
588e70
Date:   Tue Sep 29 15:40:08 2020 -0300
588e70
588e70
    powerpc: Add optimized strlen for POWER10
588e70
    
588e70
    Improvements compared to POWER9 version:
588e70
    
588e70
    1. Take into account first 16B comparison for aligned strings
588e70
    
588e70
       The previous version compares the first 16B and increments r4 by the number
588e70
       of bytes until the address is 16B-aligned, then starts doing aligned loads at
588e70
       that address. For aligned strings, this causes the first 16B to be compared
588e70
       twice, because the increment is 0. Here we calculate the next 16B-aligned
588e70
       address differently, which avoids that issue.
588e70
    
588e70
    2. Use simple comparisons for the first ~192 bytes
588e70
    
588e70
       The main loop is good for big strings, but comparing 16B each time is better
588e70
       for smaller strings.  So after aligning the address to 16 Bytes, we check
588e70
       more 176B in 16B chunks.  There may be some overlaps with the main loop for
588e70
       unaligned strings, but we avoid using the more aggressive strategy too soon,
588e70
       and also allow the loop to start at a 64B-aligned address.  This greatly
588e70
       benefits smaller strings and avoids overlapping checks if the string is
588e70
       already aligned at a 64B boundary.
588e70
    
588e70
    3. Reduce dependencies between load blocks caused by address calculation on loop
588e70
    
588e70
       Doing a precise time tracing on the code showed many loads in the loop were
588e70
       stalled waiting for updates to r4 from previous code blocks.  This
588e70
       implementation avoids that as much as possible by using 2 registers (r4 and
588e70
       r5) to hold addresses to be used by different parts of the code.
588e70
    
588e70
       Also, the previous code aligned the address to 16B, then to 64B by doing a
588e70
       few 48B loops (if needed) until the address was aligned. The main loop could
588e70
       not start until that 48B loop had finished and r4 was updated with the
588e70
       current address. Here we calculate the address used by the loop very early,
588e70
       so it can start sooner.
588e70
    
588e70
       The main loop now uses 2 pointers 128B apart to make pointer updates less
588e70
       frequent, and also unrolls 1 iteration to guarantee there is enough time
588e70
       between iterations to update the pointers, reducing stalled cycles.
588e70
    
588e70
    4. Use new P10 instructions
588e70
    
588e70
       lxvp is used to load 32B with a single instruction, reducing contention in
588e70
       the load queue.
588e70
    
588e70
       vextractbm allows simplifying the tail code for the loop, replacing
588e70
       vbpermq and avoiding having to generate a permute control vector.
588e70
    
588e70
    Reviewed-by: Paul E Murphy <murphyp@linux.ibm.com>
588e70
    Reviewed-by: Raphael M Zinsly <rzinsly@linux.ibm.com>
588e70
    Reviewed-by: Lucas A. M. Magalhaes <lamm@linux.ibm.com>
588e70
588e70
diff --git a/sysdeps/powerpc/powerpc64/le/power10/strlen.S b/sysdeps/powerpc/powerpc64/le/power10/strlen.S
588e70
new file mode 100644
588e70
index 0000000000000000..ca7e9eb3d84c9b00
588e70
--- /dev/null
588e70
+++ b/sysdeps/powerpc/powerpc64/le/power10/strlen.S
588e70
@@ -0,0 +1,221 @@
588e70
+/* Optimized strlen implementation for POWER10 LE.
588e70
+   Copyright (C) 2021 Free Software Foundation, Inc.
588e70
+   This file is part of the GNU C Library.
588e70
+
588e70
+   The GNU C Library is free software; you can redistribute it and/or
588e70
+   modify it under the terms of the GNU Lesser General Public
588e70
+   License as published by the Free Software Foundation; either
588e70
+   version 2.1 of the License, or (at your option) any later version.
588e70
+
588e70
+   The GNU C Library is distributed in the hope that it will be useful,
588e70
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
588e70
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
588e70
+   Lesser General Public License for more details.
588e70
+
588e70
+   You should have received a copy of the GNU Lesser General Public
588e70
+   License along with the GNU C Library; if not, see
588e70
+   <https://www.gnu.org/licenses/>.  */
588e70
+
588e70
+#include <sysdep.h>
588e70
+
588e70
+#ifndef STRLEN
588e70
+# define STRLEN __strlen
588e70
+# define DEFINE_STRLEN_HIDDEN_DEF 1
588e70
+#endif
588e70
+
588e70
+/* TODO: Replace macros by the actual instructions when minimum binutils becomes
588e70
+   >= 2.35.  This is used to keep compatibility with older versions.  */
588e70
+#define VEXTRACTBM(rt,vrb)	 \
588e70
+	.long(((4)<<(32-6))	 \
588e70
+	      | ((rt)<<(32-11))	 \
588e70
+	      | ((8)<<(32-16))	 \
588e70
+	      | ((vrb)<<(32-21)) \
588e70
+	      | 1602)
588e70
+
588e70
+#define LXVP(xtp,dq,ra)		   \
588e70
+	.long(((6)<<(32-6))		   \
588e70
+	      | ((((xtp)-32)>>1)<<(32-10)) \
588e70
+	      | ((1)<<(32-11))		   \
588e70
+	      | ((ra)<<(32-16))		   \
588e70
+	      | dq)
588e70
+
588e70
+#define CHECK16(vreg,offset,addr,label) \
588e70
+	lxv	  vreg+32,offset(addr);	\
588e70
+	vcmpequb. vreg,vreg,v18;	\
588e70
+	bne	  cr6,L(label);
588e70
+
588e70
+/* Load 4 quadwords, merge into one VR for speed and check for NULLs.  r6 has #
588e70
+   of bytes already checked.  */
588e70
+#define CHECK64(offset,addr,label)	    \
588e70
+	li	  r6,offset;		    \
588e70
+	LXVP(v4+32,offset,addr);	    \
588e70
+	LXVP(v6+32,offset+32,addr);	    \
588e70
+	vminub	  v14,v4,v5;		    \
588e70
+	vminub	  v15,v6,v7;		    \
588e70
+	vminub	  v16,v14,v15;		    \
588e70
+	vcmpequb. v0,v16,v18;		    \
588e70
+	bne	  cr6,L(label)
588e70
+
588e70
+#define TAIL(vreg,increment)	   \
588e70
+	vctzlsbb  r4,vreg;	   \
588e70
+	subf	  r3,r3,r5;	   \
588e70
+	addi	  r4,r4,increment; \
588e70
+	add	  r3,r3,r4;	   \
588e70
+	blr
588e70
+
588e70
+/* Implements the function
588e70
+
588e70
+   int [r3] strlen (const void *s [r3])
588e70
+
588e70
+   The implementation can load bytes past a matching byte, but only
588e70
+   up to the next 64B boundary, so it never crosses a page.  */
588e70
+
588e70
+.machine power9
588e70
+
588e70
+ENTRY_TOCLESS (STRLEN, 4)
588e70
+	CALL_MCOUNT 1
588e70
+
588e70
+	vspltisb  v18,0
588e70
+	vspltisb  v19,-1
588e70
+
588e70
+	/* Next 16B-aligned address. Prepare address for L(aligned).  */
588e70
+	addi	  r5,r3,16
588e70
+	clrrdi	  r5,r5,4
588e70
+
588e70
+	/* Align data and fill bytes not loaded with non matching char.	 */
588e70
+	lvx	  v0,0,r3
588e70
+	lvsr	  v1,0,r3
588e70
+	vperm	  v0,v19,v0,v1
588e70
+
588e70
+	vcmpequb. v6,v0,v18
588e70
+	beq	  cr6,L(aligned)
588e70
+
588e70
+	vctzlsbb  r3,v6
588e70
+	blr
588e70
+
588e70
+	/* Test next 176B, 16B at a time.  The main loop is optimized for longer
588e70
+	   strings, so checking the first bytes in 16B chunks benefits a lot
588e70
+	   small strings.  */
588e70
+	.p2align 5
588e70
+L(aligned):
588e70
+	/* Prepare address for the loop.  */
588e70
+	addi	  r4,r3,192
588e70
+	clrrdi	  r4,r4,6
588e70
+
588e70
+	CHECK16(v0,0,r5,tail1)
588e70
+	CHECK16(v1,16,r5,tail2)
588e70
+	CHECK16(v2,32,r5,tail3)
588e70
+	CHECK16(v3,48,r5,tail4)
588e70
+	CHECK16(v4,64,r5,tail5)
588e70
+	CHECK16(v5,80,r5,tail6)
588e70
+	CHECK16(v6,96,r5,tail7)
588e70
+	CHECK16(v7,112,r5,tail8)
588e70
+	CHECK16(v8,128,r5,tail9)
588e70
+	CHECK16(v9,144,r5,tail10)
588e70
+	CHECK16(v10,160,r5,tail11)
588e70
+
588e70
+	addi	  r5,r4,128
588e70
+
588e70
+	/* Switch to a more aggressive approach checking 64B each time.  Use 2
588e70
+	   pointers 128B apart and unroll the loop once to make the pointer
588e70
+	   updates and usages separated enough to avoid stalls waiting for
588e70
+	   address calculation.  */
588e70
+	.p2align 5
588e70
+L(loop):
588e70
+	CHECK64(0,r4,pre_tail_64b)
588e70
+	CHECK64(64,r4,pre_tail_64b)
588e70
+	addi	  r4,r4,256
588e70
+
588e70
+	CHECK64(0,r5,tail_64b)
588e70
+	CHECK64(64,r5,tail_64b)
588e70
+	addi	  r5,r5,256
588e70
+
588e70
+	b	  L(loop)
588e70
+
588e70
+	.p2align  5
588e70
+L(pre_tail_64b):
588e70
+	mr	r5,r4
588e70
+L(tail_64b):
588e70
+	/* OK, we found a null byte.  Let's look for it in the current 64-byte
588e70
+	   block and mark it in its corresponding VR.  lxvp vx,0(ry) puts the
588e70
+	   low 16B bytes into vx+1, and the high into vx, so the order here is
588e70
+	   v5, v4, v7, v6.  */
588e70
+	vcmpequb  v1,v5,v18
588e70
+	vcmpequb  v2,v4,v18
588e70
+	vcmpequb  v3,v7,v18
588e70
+	vcmpequb  v4,v6,v18
588e70
+
588e70
+	/* Take into account the other 64B blocks we had already checked.  */
588e70
+	add	r5,r5,r6
588e70
+
588e70
+	/* Extract first bit of each byte.  */
588e70
+	VEXTRACTBM(r7,v1)
588e70
+	VEXTRACTBM(r8,v2)
588e70
+	VEXTRACTBM(r9,v3)
588e70
+	VEXTRACTBM(r10,v4)
588e70
+
588e70
+	/* Shift each value into their corresponding position.  */
588e70
+	sldi	  r8,r8,16
588e70
+	sldi	  r9,r9,32
588e70
+	sldi	  r10,r10,48
588e70
+
588e70
+	/* Merge the results.  */
588e70
+	or	  r7,r7,r8
588e70
+	or	  r8,r9,r10
588e70
+	or	  r10,r8,r7
588e70
+
588e70
+	cnttzd	  r0,r10	  /* Count trailing zeros before the match.  */
588e70
+	subf	  r5,r3,r5
588e70
+	add	  r3,r5,r0	  /* Compute final length.  */
588e70
+	blr
588e70
+
588e70
+	.p2align  5
588e70
+L(tail1):
588e70
+	TAIL(v0,0)
588e70
+
588e70
+	.p2align  5
588e70
+L(tail2):
588e70
+	TAIL(v1,16)
588e70
+
588e70
+	.p2align  5
588e70
+L(tail3):
588e70
+	TAIL(v2,32)
588e70
+
588e70
+	.p2align  5
588e70
+L(tail4):
588e70
+	TAIL(v3,48)
588e70
+
588e70
+	.p2align  5
588e70
+L(tail5):
588e70
+	TAIL(v4,64)
588e70
+
588e70
+	.p2align  5
588e70
+L(tail6):
588e70
+	TAIL(v5,80)
588e70
+
588e70
+	.p2align  5
588e70
+L(tail7):
588e70
+	TAIL(v6,96)
588e70
+
588e70
+	.p2align  5
588e70
+L(tail8):
588e70
+	TAIL(v7,112)
588e70
+
588e70
+	.p2align  5
588e70
+L(tail9):
588e70
+	TAIL(v8,128)
588e70
+
588e70
+	.p2align  5
588e70
+L(tail10):
588e70
+	TAIL(v9,144)
588e70
+
588e70
+	.p2align  5
588e70
+L(tail11):
588e70
+	TAIL(v10,160)
588e70
+
588e70
+END (STRLEN)
588e70
+
588e70
+#ifdef DEFINE_STRLEN_HIDDEN_DEF
588e70
+weak_alias (__strlen, strlen)
588e70
+libc_hidden_builtin_def (strlen)
588e70
+#endif
588e70
diff --git a/sysdeps/powerpc/powerpc64/multiarch/Makefile b/sysdeps/powerpc/powerpc64/multiarch/Makefile
588e70
index a9e13e05e90601cd..61652b65dd223018 100644
588e70
--- a/sysdeps/powerpc/powerpc64/multiarch/Makefile
588e70
+++ b/sysdeps/powerpc/powerpc64/multiarch/Makefile
588e70
@@ -33,7 +33,8 @@ sysdep_routines += memcpy-power8-cached memcpy-power7 memcpy-a2 memcpy-power6 \
588e70
 
588e70
 ifneq (,$(filter %le,$(config-machine)))
588e70
 sysdep_routines += strcmp-power9 strncmp-power9 strcpy-power9 stpcpy-power9 \
588e70
-		   rawmemchr-power9 strlen-power9 strncpy-power9 stpncpy-power9
588e70
+		   rawmemchr-power9 strlen-power9 strncpy-power9 stpncpy-power9 \
588e70
+		   strlen-power10
588e70
 endif
588e70
 CFLAGS-strncase-power7.c += -mcpu=power7 -funroll-loops
588e70
 CFLAGS-strncase_l-power7.c += -mcpu=power7 -funroll-loops
588e70
diff --git a/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c b/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
588e70
index b30bc53930fc0e36..46d5956adda72b86 100644
588e70
--- a/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
588e70
+++ b/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
588e70
@@ -112,6 +112,8 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
588e70
   /* Support sysdeps/powerpc/powerpc64/multiarch/strlen.c.  */
588e70
   IFUNC_IMPL (i, name, strlen,
588e70
 #ifdef __LITTLE_ENDIAN__
588e70
+	      IFUNC_IMPL_ADD (array, i, strlen, hwcap2 & PPC_FEATURE2_ARCH_3_1,
588e70
+			      __strlen_power10)
588e70
 	      IFUNC_IMPL_ADD (array, i, strlen, hwcap2 & PPC_FEATURE2_ARCH_3_00,
588e70
 			      __strlen_power9)
588e70
 #endif
588e70
diff --git a/sysdeps/powerpc/powerpc64/multiarch/strlen-power10.S b/sysdeps/powerpc/powerpc64/multiarch/strlen-power10.S
588e70
new file mode 100644
588e70
index 0000000000000000..6a774fad58c77179
588e70
--- /dev/null
588e70
+++ b/sysdeps/powerpc/powerpc64/multiarch/strlen-power10.S
588e70
@@ -0,0 +1,2 @@
588e70
+#define STRLEN __strlen_power10
588e70
+#include <sysdeps/powerpc/powerpc64/le/power10/strlen.S>
588e70
diff --git a/sysdeps/powerpc/powerpc64/multiarch/strlen.c b/sysdeps/powerpc/powerpc64/multiarch/strlen.c
588e70
index b7f0fbb13fb97783..11bdb96de2d2aa66 100644
588e70
--- a/sysdeps/powerpc/powerpc64/multiarch/strlen.c
588e70
+++ b/sysdeps/powerpc/powerpc64/multiarch/strlen.c
588e70
@@ -31,9 +31,12 @@ extern __typeof (__redirect_strlen) __strlen_ppc attribute_hidden;
588e70
 extern __typeof (__redirect_strlen) __strlen_power7 attribute_hidden;
588e70
 extern __typeof (__redirect_strlen) __strlen_power8 attribute_hidden;
588e70
 extern __typeof (__redirect_strlen) __strlen_power9 attribute_hidden;
588e70
+extern __typeof (__redirect_strlen) __strlen_power10 attribute_hidden;
588e70
 
588e70
 libc_ifunc (__libc_strlen,
588e70
 # ifdef __LITTLE_ENDIAN__
588e70
+	(hwcap2 & PPC_FEATURE2_ARCH_3_1)
588e70
+	? __strlen_power10 :
588e70
 	  (hwcap2 & PPC_FEATURE2_ARCH_3_00)
588e70
 	  ? __strlen_power9 :
588e70
 # endif