Blame SOURCES/ia-code_cleanup-strchr-avx2.patch

513694
From 1da58a6d12719da8cc2035c2f6f9928d2ad61a20 Mon Sep 17 00:00:00 2001
513694
From: Noah Goldstein <goldstein.w.n@gmail.com>
513694
Date: Wed, 23 Mar 2022 16:57:16 -0500
513694
Subject: [PATCH] x86: Code cleanup in strchr-avx2 and comment justifying
513694
 branch
513694
513694
Small code cleanup for size: -53 bytes.
513694
513694
Add comment justifying using a branch to do NULL/non-null return.
513694
513694
All string/memory tests pass and no regressions in benchtests.
513694
513694
geometric_mean(N=20) of all benchmarks Original / New: 1.00
513694
Reviewed-by: H.J. Lu <hjl.tools@gmail.com>
513694
513694
(cherry picked from commit a6fbf4d51e9ba8063c4f8331564892ead9c67344)
513694
---
513694
 sysdeps/x86_64/multiarch/strchr-avx2.S | 204 +++++++++++++------------
513694
 1 file changed, 107 insertions(+), 97 deletions(-)
513694
513694
diff --git a/sysdeps/x86_64/multiarch/strchr-avx2.S b/sysdeps/x86_64/multiarch/strchr-avx2.S
513694
index 5884726b..89dd2bf7 100644
513694
--- a/sysdeps/x86_64/multiarch/strchr-avx2.S
513694
+++ b/sysdeps/x86_64/multiarch/strchr-avx2.S
513694
@@ -48,13 +48,13 @@
513694
 # define PAGE_SIZE 4096
513694
 
513694
 	.section SECTION(.text),"ax",@progbits
513694
-ENTRY (STRCHR)
513694
+ENTRY_P2ALIGN (STRCHR, 5)
513694
 	/* Broadcast CHAR to YMM0.	*/
513694
 	vmovd	%esi, %xmm0
513694
 	movl	%edi, %eax
513694
 	andl	$(PAGE_SIZE - 1), %eax
513694
 	VPBROADCAST	%xmm0, %ymm0
513694
-	vpxor	%xmm9, %xmm9, %xmm9
513694
+	vpxor	%xmm1, %xmm1, %xmm1
513694
 
513694
 	/* Check if we cross page boundary with one vector load.  */
513694
 	cmpl	$(PAGE_SIZE - VEC_SIZE), %eax
513694
@@ -62,37 +62,29 @@ ENTRY (STRCHR)
513694
 
513694
 	/* Check the first VEC_SIZE bytes.	Search for both CHAR and the
513694
 	   null byte.  */
513694
-	vmovdqu	(%rdi), %ymm8
513694
-	VPCMPEQ	%ymm8, %ymm0, %ymm1
513694
-	VPCMPEQ	%ymm8, %ymm9, %ymm2
513694
-	vpor	%ymm1, %ymm2, %ymm1
513694
-	vpmovmskb %ymm1, %eax
513694
+	vmovdqu	(%rdi), %ymm2
513694
+	VPCMPEQ	%ymm2, %ymm0, %ymm3
513694
+	VPCMPEQ	%ymm2, %ymm1, %ymm2
513694
+	vpor	%ymm3, %ymm2, %ymm3
513694
+	vpmovmskb %ymm3, %eax
513694
 	testl	%eax, %eax
513694
 	jz	L(aligned_more)
513694
 	tzcntl	%eax, %eax
513694
 # ifndef USE_AS_STRCHRNUL
513694
-	/* Found CHAR or the null byte.	 */
513694
-	cmp	(%rdi, %rax), %CHAR_REG
513694
-	jne	L(zero)
513694
-# endif
513694
-	addq	%rdi, %rax
513694
-	VZEROUPPER_RETURN
513694
-
513694
-	/* .p2align 5 helps keep performance more consistent if ENTRY()
513694
-	   alignment % 32 was either 16 or 0. As well this makes the
513694
-	   alignment % 32 of the loop_4x_vec fixed which makes tuning it
513694
-	   easier.  */
513694
-	.p2align 5
513694
-L(first_vec_x4):
513694
-	tzcntl	%eax, %eax
513694
-	addq	$(VEC_SIZE * 3 + 1), %rdi
513694
-# ifndef USE_AS_STRCHRNUL
513694
-	/* Found CHAR or the null byte.	 */
513694
+	/* Found CHAR or the null byte.  */
513694
 	cmp	(%rdi, %rax), %CHAR_REG
513694
+	/* NB: Use a branch instead of cmovcc here. The expectation is
513694
+	   that with strchr the user will branch based on input being
513694
+	   null. Since this branch will be 100% predictive of the user
513694
+	   branch a branch miss here should save what otherwise would
513694
+	   be branch miss in the user code. Otherwise using a branch 1)
513694
+	   saves code size and 2) is faster in highly predictable
513694
+	   environments.  */
513694
 	jne	L(zero)
513694
 # endif
513694
 	addq	%rdi, %rax
513694
-	VZEROUPPER_RETURN
513694
+L(return_vzeroupper):
513694
+	ZERO_UPPER_VEC_REGISTERS_RETURN
513694
 
513694
 # ifndef USE_AS_STRCHRNUL
513694
 L(zero):
513694
@@ -103,7 +95,8 @@ L(zero):
513694
 
513694
 	.p2align 4
513694
 L(first_vec_x1):
513694
-	tzcntl	%eax, %eax
513694
+	/* Use bsf to save code size.  */
513694
+	bsfl	%eax, %eax
513694
 	incq	%rdi
513694
 # ifndef USE_AS_STRCHRNUL
513694
 	/* Found CHAR or the null byte.	 */
513694
@@ -113,9 +106,10 @@ L(first_vec_x1):
513694
 	addq	%rdi, %rax
513694
 	VZEROUPPER_RETURN
513694
 
513694
-	.p2align 4
513694
+	.p2align 4,, 10
513694
 L(first_vec_x2):
513694
-	tzcntl	%eax, %eax
513694
+	/* Use bsf to save code size.  */
513694
+	bsfl	%eax, %eax
513694
 	addq	$(VEC_SIZE + 1), %rdi
513694
 # ifndef USE_AS_STRCHRNUL
513694
 	/* Found CHAR or the null byte.	 */
513694
@@ -125,9 +119,10 @@ L(first_vec_x2):
513694
 	addq	%rdi, %rax
513694
 	VZEROUPPER_RETURN
513694
 
513694
-	.p2align 4
513694
+	.p2align 4,, 8
513694
 L(first_vec_x3):
513694
-	tzcntl	%eax, %eax
513694
+	/* Use bsf to save code size.  */
513694
+	bsfl	%eax, %eax
513694
 	addq	$(VEC_SIZE * 2 + 1), %rdi
513694
 # ifndef USE_AS_STRCHRNUL
513694
 	/* Found CHAR or the null byte.	 */
513694
@@ -137,6 +132,21 @@ L(first_vec_x3):
513694
 	addq	%rdi, %rax
513694
 	VZEROUPPER_RETURN
513694
 
513694
+	.p2align 4,, 10
513694
+L(first_vec_x4):
513694
+	/* Use bsf to save code size.  */
513694
+	bsfl	%eax, %eax
513694
+	addq	$(VEC_SIZE * 3 + 1), %rdi
513694
+# ifndef USE_AS_STRCHRNUL
513694
+	/* Found CHAR or the null byte.	 */
513694
+	cmp	(%rdi, %rax), %CHAR_REG
513694
+	jne	L(zero)
513694
+# endif
513694
+	addq	%rdi, %rax
513694
+	VZEROUPPER_RETURN
513694
+
513694
+
513694
+
513694
 	.p2align 4
513694
 L(aligned_more):
513694
 	/* Align data to VEC_SIZE - 1. This is the same number of
513694
@@ -146,90 +156,92 @@ L(aligned_more):
513694
 L(cross_page_continue):
513694
 	/* Check the next 4 * VEC_SIZE.  Only one VEC_SIZE at a time
513694
 	   since data is only aligned to VEC_SIZE.  */
513694
-	vmovdqa	1(%rdi), %ymm8
513694
-	VPCMPEQ	%ymm8, %ymm0, %ymm1
513694
-	VPCMPEQ	%ymm8, %ymm9, %ymm2
513694
-	vpor	%ymm1, %ymm2, %ymm1
513694
-	vpmovmskb %ymm1, %eax
513694
+	vmovdqa	1(%rdi), %ymm2
513694
+	VPCMPEQ	%ymm2, %ymm0, %ymm3
513694
+	VPCMPEQ	%ymm2, %ymm1, %ymm2
513694
+	vpor	%ymm3, %ymm2, %ymm3
513694
+	vpmovmskb %ymm3, %eax
513694
 	testl	%eax, %eax
513694
 	jnz	L(first_vec_x1)
513694
 
513694
-	vmovdqa	(VEC_SIZE + 1)(%rdi), %ymm8
513694
-	VPCMPEQ	%ymm8, %ymm0, %ymm1
513694
-	VPCMPEQ	%ymm8, %ymm9, %ymm2
513694
-	vpor	%ymm1, %ymm2, %ymm1
513694
-	vpmovmskb %ymm1, %eax
513694
+	vmovdqa	(VEC_SIZE + 1)(%rdi), %ymm2
513694
+	VPCMPEQ	%ymm2, %ymm0, %ymm3
513694
+	VPCMPEQ	%ymm2, %ymm1, %ymm2
513694
+	vpor	%ymm3, %ymm2, %ymm3
513694
+	vpmovmskb %ymm3, %eax
513694
 	testl	%eax, %eax
513694
 	jnz	L(first_vec_x2)
513694
 
513694
-	vmovdqa	(VEC_SIZE * 2 + 1)(%rdi), %ymm8
513694
-	VPCMPEQ	%ymm8, %ymm0, %ymm1
513694
-	VPCMPEQ	%ymm8, %ymm9, %ymm2
513694
-	vpor	%ymm1, %ymm2, %ymm1
513694
-	vpmovmskb %ymm1, %eax
513694
+	vmovdqa	(VEC_SIZE * 2 + 1)(%rdi), %ymm2
513694
+	VPCMPEQ	%ymm2, %ymm0, %ymm3
513694
+	VPCMPEQ	%ymm2, %ymm1, %ymm2
513694
+	vpor	%ymm3, %ymm2, %ymm3
513694
+	vpmovmskb %ymm3, %eax
513694
 	testl	%eax, %eax
513694
 	jnz	L(first_vec_x3)
513694
 
513694
-	vmovdqa	(VEC_SIZE * 3 + 1)(%rdi), %ymm8
513694
-	VPCMPEQ	%ymm8, %ymm0, %ymm1
513694
-	VPCMPEQ	%ymm8, %ymm9, %ymm2
513694
-	vpor	%ymm1, %ymm2, %ymm1
513694
-	vpmovmskb %ymm1, %eax
513694
+	vmovdqa	(VEC_SIZE * 3 + 1)(%rdi), %ymm2
513694
+	VPCMPEQ	%ymm2, %ymm0, %ymm3
513694
+	VPCMPEQ	%ymm2, %ymm1, %ymm2
513694
+	vpor	%ymm3, %ymm2, %ymm3
513694
+	vpmovmskb %ymm3, %eax
513694
 	testl	%eax, %eax
513694
 	jnz	L(first_vec_x4)
513694
-	/* Align data to VEC_SIZE * 4 - 1.	*/
513694
-	addq	$(VEC_SIZE * 4 + 1), %rdi
513694
-	andq	$-(VEC_SIZE * 4), %rdi
513694
+	/* Align data to VEC_SIZE * 4 - 1.  */
513694
+	incq	%rdi
513694
+	orq	$(VEC_SIZE * 4 - 1), %rdi
513694
 	.p2align 4
513694
 L(loop_4x_vec):
513694
 	/* Compare 4 * VEC at a time forward.  */
513694
-	vmovdqa	(%rdi), %ymm5
513694
-	vmovdqa	(VEC_SIZE)(%rdi), %ymm6
513694
-	vmovdqa	(VEC_SIZE * 2)(%rdi), %ymm7
513694
-	vmovdqa	(VEC_SIZE * 3)(%rdi), %ymm8
513694
+	vmovdqa	1(%rdi), %ymm6
513694
+	vmovdqa	(VEC_SIZE + 1)(%rdi), %ymm7
513694
 
513694
 	/* Leaves only CHARS matching esi as 0.	 */
513694
-	vpxor	%ymm5, %ymm0, %ymm1
513694
 	vpxor	%ymm6, %ymm0, %ymm2
513694
 	vpxor	%ymm7, %ymm0, %ymm3
513694
-	vpxor	%ymm8, %ymm0, %ymm4
513694
 
513694
-	VPMINU	%ymm1, %ymm5, %ymm1
513694
 	VPMINU	%ymm2, %ymm6, %ymm2
513694
 	VPMINU	%ymm3, %ymm7, %ymm3
513694
-	VPMINU	%ymm4, %ymm8, %ymm4
513694
 
513694
-	VPMINU	%ymm1, %ymm2, %ymm5
513694
-	VPMINU	%ymm3, %ymm4, %ymm6
513694
+	vmovdqa	(VEC_SIZE * 2 + 1)(%rdi), %ymm6
513694
+	vmovdqa	(VEC_SIZE * 3 + 1)(%rdi), %ymm7
513694
+
513694
+	vpxor	%ymm6, %ymm0, %ymm4
513694
+	vpxor	%ymm7, %ymm0, %ymm5
513694
+
513694
+	VPMINU	%ymm4, %ymm6, %ymm4
513694
+	VPMINU	%ymm5, %ymm7, %ymm5
513694
 
513694
-	VPMINU	%ymm5, %ymm6, %ymm6
513694
+	VPMINU	%ymm2, %ymm3, %ymm6
513694
+	VPMINU	%ymm4, %ymm5, %ymm7
513694
 
513694
-	VPCMPEQ	%ymm6, %ymm9, %ymm6
513694
-	vpmovmskb %ymm6, %ecx
513694
+	VPMINU	%ymm6, %ymm7, %ymm7
513694
+
513694
+	VPCMPEQ	%ymm7, %ymm1, %ymm7
513694
+	vpmovmskb %ymm7, %ecx
513694
 	subq	$-(VEC_SIZE * 4), %rdi
513694
 	testl	%ecx, %ecx
513694
 	jz	L(loop_4x_vec)
513694
 
513694
-
513694
-	VPCMPEQ	%ymm1, %ymm9, %ymm1
513694
-	vpmovmskb %ymm1, %eax
513694
+	VPCMPEQ	%ymm2, %ymm1, %ymm2
513694
+	vpmovmskb %ymm2, %eax
513694
 	testl	%eax, %eax
513694
 	jnz	L(last_vec_x0)
513694
 
513694
 
513694
-	VPCMPEQ	%ymm5, %ymm9, %ymm2
513694
-	vpmovmskb %ymm2, %eax
513694
+	VPCMPEQ	%ymm3, %ymm1, %ymm3
513694
+	vpmovmskb %ymm3, %eax
513694
 	testl	%eax, %eax
513694
 	jnz	L(last_vec_x1)
513694
 
513694
-	VPCMPEQ	%ymm3, %ymm9, %ymm3
513694
-	vpmovmskb %ymm3, %eax
513694
+	VPCMPEQ	%ymm4, %ymm1, %ymm4
513694
+	vpmovmskb %ymm4, %eax
513694
 	/* rcx has combined result from all 4 VEC. It will only be used
513694
 	   if the first 3 other VEC all did not contain a match.  */
513694
 	salq	$32, %rcx
513694
 	orq	%rcx, %rax
513694
 	tzcntq	%rax, %rax
513694
-	subq	$(VEC_SIZE * 2), %rdi
513694
+	subq	$(VEC_SIZE * 2 - 1), %rdi
513694
 # ifndef USE_AS_STRCHRNUL
513694
 	/* Found CHAR or the null byte.	 */
513694
 	cmp	(%rdi, %rax), %CHAR_REG
513694
@@ -239,10 +251,11 @@ L(loop_4x_vec):
513694
 	VZEROUPPER_RETURN
513694
 
513694
 
513694
-	.p2align 4
513694
+	.p2align 4,, 10
513694
 L(last_vec_x0):
513694
-	tzcntl	%eax, %eax
513694
-	addq	$-(VEC_SIZE * 4), %rdi
513694
+	/* Use bsf to save code size.  */
513694
+	bsfl	%eax, %eax
513694
+	addq	$-(VEC_SIZE * 4 - 1), %rdi
513694
 # ifndef USE_AS_STRCHRNUL
513694
 	/* Found CHAR or the null byte.	 */
513694
 	cmp	(%rdi, %rax), %CHAR_REG
513694
@@ -251,16 +264,11 @@ L(last_vec_x0):
513694
 	addq	%rdi, %rax
513694
 	VZEROUPPER_RETURN
513694
 
513694
-# ifndef USE_AS_STRCHRNUL
513694
-L(zero_end):
513694
-	xorl	%eax, %eax
513694
-	VZEROUPPER_RETURN
513694
-# endif
513694
 
513694
-	.p2align 4
513694
+	.p2align 4,, 10
513694
 L(last_vec_x1):
513694
 	tzcntl	%eax, %eax
513694
-	subq	$(VEC_SIZE * 3), %rdi
513694
+	subq	$(VEC_SIZE * 3 - 1), %rdi
513694
 # ifndef USE_AS_STRCHRNUL
513694
 	/* Found CHAR or the null byte.	 */
513694
 	cmp	(%rdi, %rax), %CHAR_REG
513694
@@ -269,18 +277,23 @@ L(last_vec_x1):
513694
 	addq	%rdi, %rax
513694
 	VZEROUPPER_RETURN
513694
 
513694
+# ifndef USE_AS_STRCHRNUL
513694
+L(zero_end):
513694
+	xorl	%eax, %eax
513694
+	VZEROUPPER_RETURN
513694
+# endif
513694
 
513694
 	/* Cold case for crossing page with first load.	 */
513694
-	.p2align 4
513694
+	.p2align 4,, 8
513694
 L(cross_page_boundary):
513694
 	movq	%rdi, %rdx
513694
 	/* Align rdi to VEC_SIZE - 1.  */
513694
 	orq	$(VEC_SIZE - 1), %rdi
513694
-	vmovdqa	-(VEC_SIZE - 1)(%rdi), %ymm8
513694
-	VPCMPEQ	%ymm8, %ymm0, %ymm1
513694
-	VPCMPEQ	%ymm8, %ymm9, %ymm2
513694
-	vpor	%ymm1, %ymm2, %ymm1
513694
-	vpmovmskb %ymm1, %eax
513694
+	vmovdqa	-(VEC_SIZE - 1)(%rdi), %ymm2
513694
+	VPCMPEQ	%ymm2, %ymm0, %ymm3
513694
+	VPCMPEQ	%ymm2, %ymm1, %ymm2
513694
+	vpor	%ymm3, %ymm2, %ymm3
513694
+	vpmovmskb %ymm3, %eax
513694
 	/* Remove the leading bytes. sarxl only uses bits [5:0] of COUNT
513694
 	   so no need to manually mod edx.  */
513694
 	sarxl	%edx, %eax, %eax
513694
@@ -291,13 +304,10 @@ L(cross_page_boundary):
513694
 	xorl	%ecx, %ecx
513694
 	/* Found CHAR or the null byte.	 */
513694
 	cmp	(%rdx, %rax), %CHAR_REG
513694
-	leaq	(%rdx, %rax), %rax
513694
-	cmovne	%rcx, %rax
513694
-# else
513694
-	addq	%rdx, %rax
513694
+	jne	L(zero_end)
513694
 # endif
513694
-L(return_vzeroupper):
513694
-	ZERO_UPPER_VEC_REGISTERS_RETURN
513694
+	addq	%rdx, %rax
513694
+	VZEROUPPER_RETURN
513694
 
513694
 END (STRCHR)
513694
-# endif
513694
+#endif
513694
-- 
513694
GitLab
513694