From: Kyrylo Tkachov <[email protected]>
memchr has no RTL expansion, so a target that can search the first bytes
of a buffer cheaply cannot handle short searches inline. This patch adds
a memchr<m> optab and expands the builtin through it when the target has a
pattern. The length is a limit on the search, not a readable size, so the
memory operand has no size.
md.texi documents the new pattern.
No target defines the new pattern in this patch.
Bootstrapped and tested on aarch64-none-linux-gnu.
Ok for trunk?
Thanks,
Kyrill
gcc/ChangeLog:
* builtins.cc (expand_builtin_memchr): New function.
(expand_builtin): Use it for BUILT_IN_MEMCHR.
* doc/md.texi (memchr@var{m}): Document.
* optabs.def (memchr_optab): New optab.
Signed-off-by: Kyrylo Tkachov <[email protected]>
---
gcc/builtins.cc | 42 ++++++++++++++++++++++++++++++++++++++++++
gcc/doc/md.texi | 12 ++++++++++++
gcc/optabs.def | 1 +
3 files changed, 55 insertions(+)
diff --git a/gcc/builtins.cc b/gcc/builtins.cc
index 128a12a898f..292c12a689d 100644
--- a/gcc/builtins.cc
+++ b/gcc/builtins.cc
@@ -3665,6 +3665,43 @@ determine_block_size (tree len, rtx &len_rtx,
GET_MODE_MASK (GET_MODE (len_rtx)));
}
+/* Expand a call EXP to the memchr built-in with the memchr pattern, if the
+ target has one. Return NULL_RTX if the caller should emit a normal call,
+ otherwise try to get the result in TARGET, if convenient. */
+
+static rtx
+expand_builtin_memchr (tree exp, rtx target)
+{
+ if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE,
+ VOID_TYPE))
+ return NULL_RTX;
+
+ insn_code icode = direct_optab_handler (memchr_optab, Pmode);
+ if (icode == CODE_FOR_nothing)
+ return NULL_RTX;
+
+ /* Stabilize the arguments in case the pattern fails. */
+ tree args[3];
+ for (int i = 0; i < 3; i++)
+ {
+ args[i] = builtin_save_expr (CALL_EXPR_ARG (exp, i));
+ CALL_EXPR_ARG (exp, i) = args[i];
+ }
+
+ /* The length is a limit on the search, not the size of an object that
+ can be read, so the memory reference has no size. */
+ class expand_operand ops[4];
+ create_output_operand (&ops[0], target, Pmode);
+ create_fixed_operand (&ops[1], get_memory_rtx (args[0], NULL_TREE, false));
+ create_convert_operand_from (&ops[2], expand_normal (args[1]),
+ TYPE_MODE (TREE_TYPE (args[1])), false);
+ create_convert_operand_from (&ops[3], expand_normal (args[2]),
+ TYPE_MODE (TREE_TYPE (args[2])), true);
+ if (!maybe_expand_insn (icode, 4, ops))
+ return NULL_RTX;
+ return convert_memory_address (ptr_mode, ops[0].value);
+}
+
/* Expand a call EXP to the memcpy builtin.
Return NULL_RTX if we failed, the caller should emit a normal call,
otherwise try to get the result in TARGET, if convenient (and in
@@ -8287,6 +8324,11 @@ expand_builtin (tree exp, rtx target, rtx subtarget,
machine_mode mode,
return target;
break;
+ case BUILT_IN_MEMCHR:
+ if (rtx ret = expand_builtin_memchr (exp, target))
+ return ret;
+ break;
+
case BUILT_IN_STRCPY:
target = expand_builtin_strcpy (exp, target);
if (target)
diff --git a/gcc/doc/md.texi b/gcc/doc/md.texi
index a273c2e4707..76471cd322e 100644
--- a/gcc/doc/md.texi
+++ b/gcc/doc/md.texi
@@ -6058,6 +6058,18 @@ operand 2 is the character to search for (normally zero),
and operand 3 is a constant describing the known alignment
of the beginning of the string.
+@mdindex memchr@var{m}
+@item @samp{memchr@var{m}}
+Search for the first byte equal to operand 2 in the first operand 3
+bytes of the memory referred to by operand 1. Operand 0 is the result,
+of mode @var{m}: the address of the byte, or zero if there is none.
+Operand 1 is a @code{mem:BLK} whose address has mode @var{m}. Operand 2
+has mode @code{QImode} and operand 3 has mode @var{m}. Operand 3 limits
+the search. It does not mean that the bytes after the first match can
+be read, so the expansion must only read such bytes if they cannot fault,
+for example because they lie in an aligned block that also contains a
+byte that the search has to read.
+
@mdindex rawmemchr@var{m}
@item @samp{rawmemchr@var{m}}
Scan memory referred to by operand 1 for the first occurrence of operand 2.
diff --git a/gcc/optabs.def b/gcc/optabs.def
index d6806a6156a..c1b618f7763 100644
--- a/gcc/optabs.def
+++ b/gcc/optabs.def
@@ -333,6 +333,7 @@ OPTAB_D (cpymem_optab, "cpymem$a")
OPTAB_D (movmem_optab, "movmem$a")
OPTAB_D (setmem_optab, "setmem$a")
OPTAB_D (strlen_optab, "strlen$a")
+OPTAB_D (memchr_optab, "memchr$a")
OPTAB_D (rawmemchr_optab, "rawmemchr$a")
OPTAB_DC(fma_optab, "fma$a4", FMA)
--
2.54.0 (Apple Git-157)