[DTrace-devel] [PATCH 14/14] Simplify strcmp.S

Kris Van Hees kris.van.hees at oracle.com
Sat Feb 18 06:59:59 UTC 2023


On Thu, Nov 03, 2022 at 03:52:26PM -0400, eugene.loh--- via DTrace-devel wrote:
> From: Eugene Loh <eugene.loh at oracle.com>
> 
> The code branched depending on whether the first string is known
> to be a pointer to D-managed storage.  If we simply assume that
> the storage is never D-managed, we can save two instructions as
> well as a conditional jump (the bane of the BPF verifier).  The
> execution path ends up a couple of instructions either longer or
> shorter depending on the case.  The resulting implementation is
> closer to that of other string subroutines.
> 
> Signed-off-by: Eugene Loh <eugene.loh at oracle.com>

Reviewed-by: Kris Van Hees <kris.van.hees at oracle.com>

> ---
>  bpf/strcmp.S | 31 ++++++++++++-------------------
>  1 file changed, 12 insertions(+), 19 deletions(-)
> 
> diff --git a/bpf/strcmp.S b/bpf/strcmp.S
> index cf5e48e6..8147e257 100644
> --- a/bpf/strcmp.S
> +++ b/bpf/strcmp.S
> @@ -88,12 +88,10 @@ dt_strcmp_not :
>   *     if (r0 > r6) goto Lsame;
>   *     if (r0 > r7) goto Lsame;
>   *
> - *     r6 = &s[r0];
> - *     r7 = &tmp2[r0];
> + *     r7 = tmp2[r0];
>   *
> - *     if (flags & 1) { } else { bpf_probe_read(tmp1, 1, r6); r6 = tmp1; }
> - *     r6 = *r6;
> - *     r7 = *r7;
> + *     bpf_probe_read(tmp1, 1, &s[r0]);
> + *     r6 = tmp1[0];
>   *
>   *     // if all chars are the same, break tie on string length
>   * Lsame:
> @@ -165,24 +163,19 @@ dt_strcmp :
>  	jgt	%r0, %r6, .Lsame	/* if (r0 > r6) goto Lsame */
>  	jgt	%r0, %r7, .Lsame	/* if (r0 > r7) goto Lsame */
>  
> -	ldxdw	%r6, [%fp+-8]
> -	add	%r6, %r0		/* r6 = &s[r0] */
>  	ldxdw	%r7, [%fp+-32]
> -	add	%r7, %r0		/* r7 = &tmp2[r0] */
> +	add	%r7, %r0
> +	ldxb	%r7, [%r7+0]
> +	and	%r7, 0xff		/* r7 = tmp2[r0] */
>  
> -	jset	%r9, 1, .Ls_is_dptr
>  	ldxdw	%r1, [%fp+-24]
>  	mov	%r2, 1
> -	mov	%r3, %r6
> -	mov	%r6, %r1		/* r6 = tmp1 (after the helper call) */
> -	call	BPF_FUNC_probe_read	/* bpf_probe_read(tmp1, 1, r6) */
> -.Ls_is_dptr:
> -
> -	ldxb	%r6, [%r6+0]		/* r6 = *r6 */
> -	and	%r6, 0xff
> -
> -	ldxb	%r7, [%r7+0]		/* r7 = *r7 */
> -	and	%r7, 0xff
> +	ldxdw	%r3, [%fp+-8]
> +	add	%r3, %r0
> +	call	BPF_FUNC_probe_read	/* bpf_probe_read(tmp1, 1, &s[r0]) */
> +	ldxdw	%r6, [%fp+-24]
> +	ldxb	%r6, [%r6+0]
> +	and	%r6, 0xff		/* r6 = tmp1[0] */
>  
>  .Lsame:
>  	jle	%r6, %r7, 2		/* if (r6 > r7) return +1 */
> -- 
> 2.18.4
> 
> 
> _______________________________________________
> DTrace-devel mailing list
> DTrace-devel at oss.oracle.com
> https://oss.oracle.com/mailman/listinfo/dtrace-devel



More information about the DTrace-devel mailing list