ce426f
From 2ffa8b8660a7a17572ae5a398171c8be59985eb3 Mon Sep 17 00:00:00 2001
ce426f
From: "Gabriel F. T. Gomes" <gftg@linux.vnet.ibm.com>
ce426f
Date: Mon, 25 Jan 2016 10:50:34 -0500
ce426f
Subject: [PATCH] powerpc: Zero pad using memset in strncpy/stpncpy
ce426f
ce426f
Call __memset_power8 to pad, with zeros, the remaining bytes in the
ce426f
dest string on __strncpy_power8 and __stpncpy_power8.  This improves
ce426f
performance when n is larger than the input string, giving ~30% gain for
ce426f
larger strings without impacting much shorter strings.
ce426f
ce426f
(cherry picked from commit 72c11b353ede72931cc474c9071d143d9a05c0d7)
ce426f
---
ce426f
 ChangeLog                                  |   5 ++
ce426f
 sysdeps/powerpc/powerpc64/power8/strncpy.S | 123 +++++++++++++----------------
ce426f
 2 files changed, 61 insertions(+), 67 deletions(-)
ce426f
ce426f
diff --git a/ChangeLog b/ChangeLog
ce426f
index 5537fc6..8d0e296 100644
ce426f
diff --git a/sysdeps/powerpc/powerpc64/power8/strncpy.S b/sysdeps/powerpc/powerpc64/power8/strncpy.S
ce426f
index 5fda953..80136cc 100644
ce426f
--- a/sysdeps/powerpc/powerpc64/power8/strncpy.S
ce426f
+++ b/sysdeps/powerpc/powerpc64/power8/strncpy.S
ce426f
@@ -24,6 +24,8 @@
ce426f
 # define FUNC_NAME strncpy
ce426f
 #endif
ce426f
 
ce426f
+#define FRAMESIZE (FRAME_MIN_SIZE+48)
ce426f
+
ce426f
 /* Implements the function
ce426f
 
ce426f
    char * [r3] strncpy (char *dest [r3], const char *src [r4], size_t n [r5])
ce426f
@@ -54,8 +56,7 @@ EALIGN (FUNC_NAME, 4, 0)
ce426f
 	addi	r10,r4,16
ce426f
 	rlwinm	r9,r4,0,19,19
ce426f
 
ce426f
-	/* Since it is a leaf function, save some non-volatile registers on the
ce426f
-	   protected/red zone.  */
ce426f
+	/* Save some non-volatile registers on the stack.  */
ce426f
 	std	r26,-48(r1)
ce426f
 	std	r27,-40(r1)
ce426f
 
ce426f
@@ -69,6 +70,14 @@ EALIGN (FUNC_NAME, 4, 0)
ce426f
 	std	r30,-16(r1)
ce426f
 	std	r31,-8(r1)
ce426f
 
ce426f
+	/* Update CFI.  */
ce426f
+	cfi_offset(r26, -48)
ce426f
+	cfi_offset(r27, -40)
ce426f
+	cfi_offset(r28, -32)
ce426f
+	cfi_offset(r29, -24)
ce426f
+	cfi_offset(r30, -16)
ce426f
+	cfi_offset(r31, -8)
ce426f
+
ce426f
 	beq	cr7,L(unaligned_lt_16)
ce426f
 	rldicl	r9,r4,0,61
ce426f
 	subfic	r8,r9,8
ce426f
@@ -144,74 +153,58 @@ L(short_path_loop_end):
ce426f
 	ld	r31,-8(r1)
ce426f
 	blr
ce426f
 
ce426f
-	/* This code pads the remainder dest with NULL bytes.  The algorithm
ce426f
-	   calculate the remanining size and issues a doubleword unrolled
ce426f
-	   loops followed by a byte a byte set.  */
ce426f
+	/* This code pads the remainder of dest with NULL bytes.  The algorithm
ce426f
+	   calculates the remaining size and calls memset.  */
ce426f
 	.align	4
ce426f
 L(zero_pad_start):
ce426f
 	mr	r5,r10
ce426f
 	mr	r9,r6
ce426f
 L(zero_pad_start_1):
ce426f
-	srdi.	r8,r5,r3
ce426f
-	mr	r10,r9
ce426f
-#ifdef USE_AS_STPNCPY
ce426f
-	mr	r3,r9
ce426f
+	/* At this point:
ce426f
+	     - r5 holds the number of bytes that still have to be written to
ce426f
+	       dest.
ce426f
+	     - r9 points to the position, in dest, where the first null byte
ce426f
+	       will be written.
ce426f
+	   The above statements are true both when control reaches this label
ce426f
+	   from a branch or when falling through the previous lines.  */
ce426f
+#ifndef USE_AS_STPNCPY
ce426f
+	mr	r30,r3       /* Save the return value of strncpy.  */
ce426f
+#endif
ce426f
+	/* Prepare the call to memset.  */
ce426f
+	mr	r3,r9        /* Pointer to the area to be zero-filled.  */
ce426f
+	li	r4,0         /* Byte to be written (zero).  */
ce426f
+
ce426f
+	/* We delayed the creation of the stack frame, as well as the saving of
ce426f
+	   the link register, because only at this point, we are sure that
ce426f
+	   doing so is actually needed.  */
ce426f
+
ce426f
+	/* Save the link register.  */
ce426f
+	mflr	r0
ce426f
+	std	r0,16(r1)
ce426f
+	cfi_offset(lr, 16)
ce426f
+
ce426f
+	/* Create the stack frame.  */
ce426f
+	stdu	r1,-FRAMESIZE(r1)
ce426f
+	cfi_adjust_cfa_offset(FRAMESIZE)
ce426f
+
ce426f
+	bl	__memset_power8
ce426f
+	nop
ce426f
+
ce426f
+	/* Restore the stack frame.  */
ce426f
+	addi	r1,r1,FRAMESIZE
ce426f
+	cfi_adjust_cfa_offset(-FRAMESIZE)
ce426f
+	/* Restore the link register.  */
ce426f
+	ld	r0,16(r1)
ce426f
+	mtlr	r0
ce426f
+
ce426f
+#ifndef USE_AS_STPNCPY
ce426f
+	mr	r3,r30       /* Restore the return value of strncpy, i.e.:
ce426f
+				dest.  For stpncpy, the return value is the
ce426f
+				same as return value of memset.  */
ce426f
 #endif
ce426f
-	beq-	cr0,L(zero_pad_loop_b_start)
ce426f
-	cmpldi	cr7,r8,1
ce426f
-	li	cr7,0
ce426f
-	std	r7,0(r9)
ce426f
-	beq	cr7,L(zero_pad_loop_b_prepare)
ce426f
-	addic.	r8,r8,-2
ce426f
-	addi	r10,r9,r16
ce426f
-	std	r7,8(r9)
ce426f
-	beq	cr0,L(zero_pad_loop_dw_2)
ce426f
-	std	r7,16(r9)
ce426f
-	li	r9,0
ce426f
-	b	L(zero_pad_loop_dw_1)
ce426f
-
ce426f
-	.align	4
ce426f
-L(zero_pad_loop_dw):
ce426f
-	addi	r10,r10,16
ce426f
-	std	r9,-8(r10)
ce426f
-	beq	cr0,L(zero_pad_loop_dw_2)
ce426f
-	std	r9,0(r10)
ce426f
-L(zero_pad_loop_dw_1):
ce426f
-	cmpldi	cr7,r8,1
ce426f
-	std	r9,0(r10)
ce426f
-	addic.	r8,r8,-2
ce426f
-	bne	cr7,L(zero_pad_loop_dw)
ce426f
-	addi	r10,r10,8
ce426f
-L(zero_pad_loop_dw_2):
ce426f
-	rldicl	r5,r5,0,61
ce426f
-L(zero_pad_loop_b_start):
ce426f
-	cmpdi	cr7,r5,0
ce426f
-	addi	r5,r5,-1
ce426f
-	addi	r9,r10,-1
ce426f
-	add	r10,r10,5
ce426f
-	subf	r10,r9,r10
ce426f
-	li	r8,0
ce426f
-	beq-	cr7,L(short_path_loop_end)
ce426f
-
ce426f
-	/* Write remaining 1-8 bytes.  */
ce426f
-        .align  4
ce426f
-	addi	r9,r9,1
ce426f
-	mtocrf	0x1,r10
ce426f
-	bf	29,4f
ce426f
-        stw     r8,0(r9)
ce426f
-        addi	r9,r9,4
ce426f
-
ce426f
-        .align  4
ce426f
-4:      bf      30,2f
ce426f
-        sth     r8,0(r9)
ce426f
-        addi	r9,r9,2
ce426f
-
ce426f
-        .align  4
ce426f
-2:      bf	31,1f
ce426f
-        stb	r8,0(r9)
ce426f
 
ce426f
-	/* Restore non-volatile registers.  */
ce426f
-1:	ld	r26,-48(r1)
ce426f
+	/* Restore non-volatile registers and return.  */
ce426f
+	ld	r26,-48(r1)
ce426f
 	ld	r27,-40(r1)
ce426f
 	ld	r28,-32(r1)
ce426f
 	ld	r29,-24(r1)
ce426f
@@ -407,10 +400,6 @@ L(short_path_prepare_2_3):
ce426f
 	mr	r4,r28
ce426f
 	mr	r9,r29
ce426f
 	b	L(short_path_2)
ce426f
-L(zero_pad_loop_b_prepare):
ce426f
-	addi	r10,r9,8
ce426f
-	rldicl	r5,r5,0,61
ce426f
-	b	L(zero_pad_loop_b_start)
ce426f
 L(zero_pad_start_prepare_1):
ce426f
 	mr	r5,r6
ce426f
 	mr	r9,r8
ce426f
-- 
ce426f
2.1.0
ce426f