[PATCH 2/2] x86: Add cmpmemsi for -minline-all-stringops

Alexander Monakov amonakov@ispras.ru
Mon Jun 1 10:19:48 GMT 2020


On Sun, 31 May 2020, H.J. Lu via Gcc-patches wrote:

> --- a/gcc/config/i386/i386-expand.c
> +++ b/gcc/config/i386/i386-expand.c
> @@ -7656,6 +7656,90 @@ ix86_expand_set_or_cpymem (rtx dst, rtx src, rtx count_exp, rtx val_exp,
>    return true;
>  }
>  
> +/* Expand cmpstrn or memcmp.  */
> +
> +bool
> +ix86_expand_cmpstrn_or_cmpmem (rtx result, rtx src1, rtx src2,
> +			       rtx length, rtx align, bool is_cmpstrn)
> +{
> +  if (optimize_insn_for_size_p () && !TARGET_INLINE_ALL_STRINGOPS)
> +    return false;
> +
> +  /* Can't use this if the user has appropriated ecx, esi or edi.  */
> +  if (fixed_regs[CX_REG] || fixed_regs[SI_REG] || fixed_regs[DI_REG])
> +    return false;
> +
> +  if (is_cmpstrn)
> +    {
> +      /* For strncmp, length is the maximum length, which can be larger
> +	 than actual string lengths.  We can expand the cmpstrn pattern
> +	 to "repz cmpsb" only if one of the strings is a constant so
> +	 that expand_builtin_strncmp() can write the length argument to
> +	 be the minimum of the const string length and the actual length
> +	 argument.  Otherwise, "repz cmpsb" may pass the 0 byte.  */
> +      tree t1 = MEM_EXPR (src1);
> +      tree t2 = MEM_EXPR (src2);
> +      if (!((t1 && TREE_CODE (t1) == MEM_REF
> +	     && TREE_CODE (TREE_OPERAND (t1, 0)) == ADDR_EXPR
> +	     && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (t1, 0), 0))
> +		 == STRING_CST))
> +	    || (t2 && TREE_CODE (t2) == MEM_REF
> +		&& TREE_CODE (TREE_OPERAND (t2, 0)) == ADDR_EXPR
> +		&& (TREE_CODE (TREE_OPERAND (TREE_OPERAND (t2, 0), 0))
> +		    == STRING_CST))))
> +	return false;
> +    }
> +  else
> +    {
> +      /* Expand memcmp to "repz cmpsb" only for -minline-all-stringops
> +	 since "repz cmpsb" can be much slower than memcmp function
> +	 implemented with vector instructions, see
> +
> +	 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43052
> +       */
> +      if (!TARGET_INLINE_ALL_STRINGOPS)
> +	return false;
> +    }

This check seems to be misplaced, "rep cmps" is slower than either memcmp or
strcmp. The test for TARGET_INLINE_ALL_STRINGOPS should happen regardless of
is_cmpstrn, so it should go earlier in the function.

Alexander


More information about the Gcc-patches mailing list