From: Kyrylo Tkachov <[email protected]>
Many strings passed to memchr, strcmp and strncmp end or differ within
their first few bytes. For these calls the library call and the setup of
the library loop cost more than the search.
This patch adds the memchr, cmpstr and cmpstrn patterns. Each expansion
tests the bytes from the start of the string to the end of its aligned
16-byte granule with one predicated SVE load (WHILELS, LD1B, CMPEQ or
CMPNE). If the answer is in these bytes, BRKB and CNTP give its position
and no call is made. Otherwise the library function continues after the
granule. The loads do not leave the granule of the first byte, which the
library call would read too, so they cannot fault and cannot cross a
memory tag granule. strcmp (a, "abc") was a tail call:
adrp x1, .LC0
add x1, x1, :lo12:.LC0
b strcmp
and becomes
adrp x1, .LC0
orr x2, x0, 15
add x1, x1, :lo12:.LC0
sub x2, x2, x0
orr x3, x1, 15
sub x3, x3, x1
cmp x2, x3
csel x2, x2, x3, ls
whilels p7.b, xzr, x2
ld1b z31.b, p7/z, [x0]
ld1b z30.b, p7/z, [x1]
cmpeq p15.b, p7/z, z31.b, #0
cmpne p14.b, p7/z, z31.b, z30.b
orrs p15.b, p7/z, p14.b, p15.b
b.none .L14
brkb p14.b, p7/z, p15.b
cntp x2, p7, p14.b
ldrb w0, [x0, x2]
ldrb w1, [x1, x2]
sub w0, w0, w1
ret
.L14:
add x0, x0, x2
add x1, x1, x2
... // frame setup
add x1, x1, 1
add x0, x0, 1
bl strcmp
... // frame teardown
strcmp and strncmp use the shorter of the two granule tails, and strncmp
and memchr also stop at the length. An unbounded strcmp is expanded only
if one operand is a string literal. The first granule of two strings of
unknown content often does not hold the end of the comparison, and the
inline sequence then repeats its work in the library call. A literal
bounds the comparison by its length. strncmp is bounded by its length
operand.
The three routines share one expansion, aarch64_expand_string_op.
The expansions need SVE. They are not used at -O0, in streaming functions
or with -fsanitize=thread. They are not used in blocks optimized for size,
because the inline sequence is longer than the call it replaces.
AddressSanitizer builds keep the library calls.
-fsanitize=hwaddress does not disable the expansions. The inline loads are
not instrumented, so only the bytes that the library call reads are checked.
A new tuning flag, inline_string_ops, enables the expansions, also through
the JSON tuning interface. Olympus sets it.
Evaluation on Olympus compared with trunk, positive is faster (5 to 10
paired rounds each, same-binary null spread about 0.2%):
cppcheck +3.4% (instructions -2.9%)
SQLite in-memory workload +0.3%
abc +0.1%
omnetpp +0.8%
The .text grows by 0.6% for cppcheck with LTO.
Bootstrapped and tested on aarch64-none-linux-gnu.
Ok for trunk?
Thanks,
Kyrill
Cc: Tamar Christina <[email protected]>
Cc: Wilco Dijkstra <[email protected]>
gcc/ChangeLog:
* config/aarch64/aarch64-protos.h (aarch64_expand_memchr)
(aarch64_expand_cmpstr): Declare.
* config/aarch64/aarch64-tuning-flags.def (inline_string_ops): New
tuning flag.
* config/aarch64/aarch64.cc (AARCH64_MEMTAG_GRANULE_SIZE): Move
before its first use.
(aarch64_inline_string_ops_p, aarch64_string_literal_p)
(aarch64_pmode_binop, aarch64_granule_rest, aarch64_umin)
(aarch64_emit_masked_load, aarch64_emit_string_call)
(aarch64_expand_string_op, aarch64_expand_memchr)
(aarch64_expand_cmpstr): New functions.
* config/aarch64/aarch64.md (memchrdi, cmpstrsi, cmpstrnsi): New
expanders.
* config/aarch64/tuning_models/olympus.h (olympus_tunings): Set
AARCH64_EXTRA_TUNE_INLINE_STRING_OPS.
gcc/testsuite/ChangeLog:
* gcc.target/aarch64/inline-scan-1.c: New test.
* gcc.target/aarch64/inline-scan-2.c: New test.
* gcc.target/aarch64/inline-scan-3.c: New test.
* gcc.target/aarch64/inline-scan-4.c: New test.
* gcc.target/aarch64/inline-scan-5.c: New test.
* gcc.target/aarch64/inline-scan-6.c: New test.
* gcc.target/aarch64/inline-scan-7.c: New test.
* gcc.target/aarch64/inline-scan-8.c: New test.
* gcc.target/aarch64/inline-scan-9.c: New test.
* gcc.target/aarch64/inline-scan-10.c: New test.
* gcc.target/aarch64/inline-scan-11.c: New test.
* gcc.target/aarch64/inline-scan-12.c: New test.
* gcc.target/aarch64/inline-scan-13.c: New test.
* gcc.target/aarch64/inline-scan-14.c: New test.
* gcc.target/aarch64/inline-scan-15.c: New test.
* gcc.target/aarch64/inline-scan-run-1.c: New test.
* gcc.target/aarch64/inline-scan-run-2.c: New test.
* gcc.target/aarch64/inline-scan-run-3.c: New test.
* gcc.target/aarch64/inline-strcmp-1.c: New test.
* gcc.target/aarch64/inline-strcmp-2.c: New test.
* gcc.target/aarch64/inline-strcmp-run-1.c: New test.
* gcc.target/aarch64/inline-strcmp-run-2.c: New test.
* gcc.target/aarch64/inline-strcmp-run-3.c: New test.
Signed-off-by: Kyrylo Tkachov <[email protected]>
---
gcc/config/aarch64/aarch64-protos.h | 2 +
gcc/config/aarch64/aarch64-tuning-flags.def | 4 +
gcc/config/aarch64/aarch64.cc | 287 +++++++++++++++++-
gcc/config/aarch64/aarch64.md | 48 +++
gcc/config/aarch64/tuning_models/olympus.h | 3 +-
.../gcc.target/aarch64/inline-scan-1.c | 18 ++
.../gcc.target/aarch64/inline-scan-10.c | 21 ++
.../gcc.target/aarch64/inline-scan-11.c | 20 ++
.../gcc.target/aarch64/inline-scan-12.c | 30 ++
.../gcc.target/aarch64/inline-scan-13.c | 30 ++
.../gcc.target/aarch64/inline-scan-14.c | 30 ++
.../gcc.target/aarch64/inline-scan-15.c | 33 ++
.../gcc.target/aarch64/inline-scan-2.c | 30 ++
.../gcc.target/aarch64/inline-scan-3.c | 22 ++
.../gcc.target/aarch64/inline-scan-4.c | 31 ++
.../gcc.target/aarch64/inline-scan-5.c | 31 ++
.../gcc.target/aarch64/inline-scan-6.c | 47 +++
.../gcc.target/aarch64/inline-scan-7.c | 14 +
.../gcc.target/aarch64/inline-scan-8.c | 20 ++
.../gcc.target/aarch64/inline-scan-9.c | 30 ++
.../gcc.target/aarch64/inline-scan-run-1.c | 75 +++++
.../gcc.target/aarch64/inline-scan-run-2.c | 29 ++
.../gcc.target/aarch64/inline-scan-run-3.c | 4 +
.../gcc.target/aarch64/inline-strcmp-1.c | 21 ++
.../gcc.target/aarch64/inline-strcmp-2.c | 22 ++
.../gcc.target/aarch64/inline-strcmp-run-1.c | 135 ++++++++
.../gcc.target/aarch64/inline-strcmp-run-2.c | 60 ++++
.../gcc.target/aarch64/inline-strcmp-run-3.c | 4 +
28 files changed, 1099 insertions(+), 2 deletions(-)
create mode 100644 gcc/testsuite/gcc.target/aarch64/inline-scan-1.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/inline-scan-10.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/inline-scan-11.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/inline-scan-12.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/inline-scan-13.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/inline-scan-14.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/inline-scan-15.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/inline-scan-2.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/inline-scan-3.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/inline-scan-4.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/inline-scan-5.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/inline-scan-6.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/inline-scan-7.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/inline-scan-8.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/inline-scan-9.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/inline-scan-run-1.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/inline-scan-run-2.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/inline-scan-run-3.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/inline-strcmp-1.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/inline-strcmp-2.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/inline-strcmp-run-1.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/inline-strcmp-run-2.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/inline-strcmp-run-3.c
diff --git a/gcc/config/aarch64/aarch64-protos.h
b/gcc/config/aarch64/aarch64-protos.h
index 500886568f0..acfecf68a59 100644
--- a/gcc/config/aarch64/aarch64-protos.h
+++ b/gcc/config/aarch64/aarch64-protos.h
@@ -893,6 +893,8 @@ void aarch64_expand_call (rtx, rtx, rtx, bool);
bool aarch64_expand_cpymem_mops (rtx *, bool);
bool aarch64_expand_cpymem (rtx *, bool);
bool aarch64_expand_setmem (rtx *);
+bool aarch64_expand_memchr (rtx *);
+bool aarch64_expand_cmpstr (rtx *, bool);
bool aarch64_float_const_zero_rtx_p (rtx);
bool aarch64_float_const_rtx_p (rtx);
bool aarch64_const_zero_rtx_p (rtx);
diff --git a/gcc/config/aarch64/aarch64-tuning-flags.def
b/gcc/config/aarch64/aarch64-tuning-flags.def
index b29189bac72..a18e1e08f86 100644
--- a/gcc/config/aarch64/aarch64-tuning-flags.def
+++ b/gcc/config/aarch64/aarch64-tuning-flags.def
@@ -88,4 +88,8 @@ AARCH64_EXTRA_TUNING_OPTION ("avoid_zeroing_movimm",
AVOID_MOVIMM_Z)
fully-masked whilelo loops (i.e. -param=vect-partial-vector-usage=1). */
AARCH64_EXTRA_TUNING_OPTION ("prefer_sve_full_vectors",
PREFER_SVE_FULL_VECTORS)
+/* Enable the inline expansion of the first granule of memchr, strcmp and
+ strncmp. */
+AARCH64_EXTRA_TUNING_OPTION ("inline_string_ops", INLINE_STRING_OPS)
+
#undef AARCH64_EXTRA_TUNING_OPTION
diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc
index 397ffd5089d..5c6cfcc19fd 100644
--- a/gcc/config/aarch64/aarch64.cc
+++ b/gcc/config/aarch64/aarch64.cc
@@ -29972,6 +29972,292 @@ aarch64_expand_setmem (rtx *operands)
}
+#define AARCH64_MEMTAG_GRANULE_SIZE 16
+
+/* String functions test the bytes from the start of the string to the end
+ of its memory tag granule inline, using SVE predicated accesses, and call
+ the library function for the rest of the string. The loads stay within
+ the granule that contains the first byte of the string, so they cannot
+ fault or access a different memory tag granule. */
+
+/* Return true if the string functions should be expanded inline. This
+ is called while expanding the call statement, whose basic block must
+ not be optimized for size. */
+
+static bool
+aarch64_inline_string_ops_p ()
+{
+ gcc_checking_assert (currently_expanding_gimple_stmt);
+ return ((aarch64_tune_params.extra_tuning_flags
+ & AARCH64_EXTRA_TUNE_INLINE_STRING_OPS)
+ && TARGET_SVE
+ && TARGET_NON_STREAMING
+ && optimize
+ && !optimize_bb_for_size_p
+ (gimple_bb (currently_expanding_gimple_stmt))
+ && !sanitize_flags_p (SANITIZE_THREAD));
+}
+
+/* Return true if the memory reference MEM refers to a string literal. */
+
+static bool
+aarch64_string_literal_p (rtx mem)
+{
+ tree base = MEM_EXPR (mem) ? get_base_address (MEM_EXPR (mem)) : NULL_TREE;
+ return base && TREE_CODE (base) == STRING_CST;
+}
+
+/* Return X CODE Y for the Pmode values X and Y, in a new register. */
+
+static rtx
+aarch64_pmode_binop (rtx_code code, rtx x, rtx y)
+{
+ return expand_simple_binop (Pmode, code, x, y, NULL_RTX, true, OPTAB_DIRECT);
+}
+
+/* Return the number of bytes from address STR to the end of its memory tag
+ granule, minus one. The granule must fit in the 128 bits of the shortest
+ SVE vector. */
+
+static rtx
+aarch64_granule_rest (rtx str)
+{
+ gcc_checking_assert (AARCH64_MEMTAG_GRANULE_SIZE <= 16);
+ rtx last = aarch64_pmode_binop (IOR, str,
+ GEN_INT (AARCH64_MEMTAG_GRANULE_SIZE - 1));
+ return aarch64_pmode_binop (MINUS, last, str);
+}
+
+/* Return the smaller of the Pmode values X and Y, compared as unsigned. */
+
+static rtx
+aarch64_umin (rtx x, rtx y)
+{
+ rtx_comparison cmp = { LEU, x, y, Pmode };
+ rtx min = emit_conditional_move (NULL_RTX, cmp, x, y, Pmode, true);
+ gcc_assert (min);
+ return min;
+}
+
+/* Return the bytes at address ADDR that are active in PG, as a vector. */
+
+static rtx
+aarch64_emit_masked_load (rtx pg, rtx addr)
+{
+ rtx data = gen_reg_rtx (VNx16QImode);
+ rtx mem = gen_rtx_MEM (VNx16QImode, addr);
+ set_mem_align (mem, BITS_PER_UNIT);
+ emit_insn (gen_maskloadvnx16qivnx16bi (data, mem, pg,
+ CONST0_RTX (VNx16QImode)));
+ return data;
+}
+
+/* Call library function FN with the NARGS arguments in ARGS, which are
+ converted to the types of the parameters of FN. Store the result in
+ RESULT. */
+
+static void
+aarch64_emit_string_call (built_in_function fn, unsigned int nargs,
+ rtx *args, rtx result)
+{
+ tree decl = builtin_decl_implicit (fn);
+ auto_vec<tree, 3> trees;
+ tree types = TYPE_ARG_TYPES (TREE_TYPE (decl));
+ for (unsigned int i = 0; i < nargs; i++, types = TREE_CHAIN (types))
+ {
+ machine_mode mode = TYPE_MODE (TREE_VALUE (types));
+ rtx arg = force_reg (mode, convert_to_mode (mode, args[i], 0));
+ trees.safe_push (make_tree (TREE_VALUE (types), arg));
+ }
+ rtx call = expand_call (build_call_expr_loc_array (UNKNOWN_LOCATION, decl,
+ nargs, trees.address ()),
+ NULL_RTX, false);
+ convert_move (result, call, POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (decl))));
+}
+
+/* Expand the library function FN, which is memchr, strcmp or strncmp.
+ OPERANDS are taken from the pattern of FN. Examine the bytes up to the
+ end of the granule of each string, or up to the length if that is shorter.
+ For memchr the string is the memory in operand 1, the byte to find is
+ operand 2 and the length is operand 3:
+
+ orr x3, x0, 15
+ sub x3, x3, x0
+ sub x4, x2, 1
+ cmp x4, x3
+ csel x3, x4, x3, ls
+ whilels p7.b, xzr, x3
+ ld1b z31.b, p7/z, [x0]
+ mov z30.b, w1
+ cmpeq p6.b, p7/z, z31.b, z30.b
+ b.none .Lnext
+ brkb p6.b, p7/z, p6.b
+ cntp x1, p7, p6.b
+ add x0, x0, x1
+
+ For strcmp and strncmp the strings are operands 1 and 2 and the length of
+ strncmp is operand 3. The examined bytes end at the first difference or
+ zero byte:
+
+ orr x2, x0, 15
+ orr x3, x1, 15
+ sub x2, x2, x0
+ sub x3, x3, x1
+ cmp x2, x3
+ csel x2, x2, x3, ls
+ whilels p7.b, xzr, x2
+ ld1b z31.b, p7/z, [x0]
+ ld1b z30.b, p7/z, [x1]
+ cmpeq p5.b, p7/z, z31.b, #0
+ cmpne p6.b, p7/z, z31.b, z30.b
+ orrs p5.b, p7/z, p6.b, p5.b
+ b.none .Lnext
+ brkb p5.b, p7/z, p5.b
+ cntp x2, p7, p5.b
+ ldrb w0, [x0, x2]
+ ldrb w1, [x1, x2]
+ sub w0, w0, w1
+
+ .Lnext returns zero if the examined bytes covered the length, and
+ otherwise calls FN for the rest of the strings. Return true on
+ success. */
+
+static bool
+aarch64_expand_string_op (built_in_function fn, rtx *operands)
+{
+ bool memchr_p = fn == BUILT_IN_MEMCHR;
+ unsigned int nstr = memchr_p ? 1 : 2;
+ rtx len = fn == BUILT_IN_STRCMP ? NULL_RTX : operands[3];
+
+ /* Two unbounded strings of unknown content rarely finish within the
+ first granule, so the inline code would only add to the library call. */
+ if (!aarch64_inline_string_ops_p ()
+ || (!len
+ && !aarch64_string_literal_p (operands[1])
+ && !aarch64_string_literal_p (operands[2])))
+ return false;
+
+ /* The result of memchr can be in the same register as the string. */
+ rtx result = gen_reg_rtx (GET_MODE (operands[0]));
+ rtx_code_label *done = gen_label_rtx ();
+ rtx len_m1 = NULL_RTX;
+ if (len)
+ {
+ emit_move_insn (result, const0_rtx);
+ emit_cmp_and_jump_insns (len, const0_rtx, EQ, NULL_RTX, Pmode, true,
+ done, profile_probability::unlikely ());
+ len_m1 = aarch64_pmode_binop (PLUS, len, constm1_rtx);
+ }
+
+ /* The offset of the last byte to examine. */
+ rtx str[2];
+ rtx offset = NULL_RTX;
+ for (unsigned int i = 0; i < nstr; i++)
+ {
+ str[i] = force_reg (Pmode, XEXP (operands[i + 1], 0));
+ rtx rest = aarch64_granule_rest (str[i]);
+ offset = offset ? aarch64_umin (offset, rest) : rest;
+ }
+ if (len)
+ offset = aarch64_umin (len_m1, offset);
+
+ rtx pg = gen_reg_rtx (VNx16BImode);
+ emit_insn (gen_while (UNSPEC_WHILELS, DImode, VNx16BImode, pg, const0_rtx,
+ offset));
+ rtx data[2];
+ for (unsigned int i = 0; i < nstr; i++)
+ data[i] = aarch64_emit_masked_load (pg, str[i]);
+
+ /* The bytes that stop the search: the byte to find for memchr, otherwise
+ a zero byte or a difference. */
+ rtx flag = gen_int_mode (SVE_MAYBE_NOT_PTRUE, SImode);
+ rtx match = gen_reg_rtx (VNx16BImode);
+ rtx value = expand_vector_broadcast (VNx16QImode,
+ memchr_p ? operands[2] : const0_rtx);
+ emit_insn (gen_aarch64_pred_cmp (EQ, VNx16QImode, match, pg, flag, data[0],
+ value));
+ rtx stop = match;
+ if (!memchr_p)
+ {
+ rtx diff = gen_reg_rtx (VNx16BImode);
+ emit_insn (gen_aarch64_pred_cmp (NE, VNx16QImode, diff, pg, flag,
+ data[0], data[1]));
+ stop = gen_reg_rtx (VNx16BImode);
+ emit_insn (gen_aarch64_pred_iorvnx16bi_z (stop, pg, diff, match));
+ }
+
+ /* Branch to NEXT if no byte stops the search. */
+ rtx_code_label *next = gen_label_rtx ();
+ emit_insn (gen_aarch64_ptestvnx16bi (pg, pg, flag, stop));
+ rtx cc = gen_rtx_REG (CC_NZCmode, CC_REGNUM);
+ rtx_insn *jump = emit_jump_insn
+ (gen_aarch64_bcond (gen_rtx_EQ (VOIDmode, cc, const0_rtx), cc, next));
+ add_reg_br_prob_note (jump, profile_probability::unlikely ());
+
+ /* The index of the first stopping byte. */
+ rtx before = gen_reg_rtx (VNx16BImode);
+ emit_insn (gen_aarch64_brk (UNSPEC_BRKB, before, pg, stop,
+ CONST0_RTX (VNx16BImode)));
+ rtx index = gen_reg_rtx (DImode);
+ emit_insn (gen_aarch64_pred_cntp (VNx16BImode, index, pg, flag, before));
+ if (memchr_p)
+ force_expand_binop (Pmode, add_optab, str[0], index, result, true,
+ OPTAB_DIRECT);
+ else
+ {
+ /* The result is the difference of the stopping bytes. */
+ rtx bytes[2];
+ for (unsigned int i = 0; i < 2; i++)
+ bytes[i] = convert_to_mode (SImode,
+ gen_rtx_MEM (QImode,
+ gen_rtx_PLUS (Pmode, str[i],
+ index)), 1);
+ force_expand_binop (SImode, sub_optab, bytes[0], bytes[1], result, false,
+ OPTAB_DIRECT);
+ }
+ emit_jump (done);
+
+ emit_label (next);
+ if (len)
+ {
+ emit_move_insn (result, const0_rtx);
+ emit_cmp_and_jump_insns (len_m1, offset, LEU, NULL_RTX, Pmode, true,
+ done, profile_probability::even ());
+ }
+ rtx args[3];
+ unsigned int nargs = 0;
+ for (unsigned int i = 0; i < nstr; i++)
+ args[nargs++] = plus_constant (Pmode, aarch64_pmode_binop (PLUS, str[i],
+ offset), 1);
+ if (memchr_p)
+ args[nargs++] = operands[2];
+ if (len)
+ args[nargs++] = aarch64_pmode_binop (MINUS, len_m1, offset);
+ aarch64_emit_string_call (fn, nargs, args, result);
+ emit_label (done);
+ emit_move_insn (operands[0], result);
+ return true;
+}
+
+/* Expand memchr. OPERANDS are taken from the memchr pattern. Return true
+ on success. */
+
+bool
+aarch64_expand_memchr (rtx *operands)
+{
+ return aarch64_expand_string_op (BUILT_IN_MEMCHR, operands);
+}
+
+/* Expand cmpstrn if BOUNDED_P is true, otherwise cmpstr. OPERANDS are taken
+ from the pattern. Return true on success. */
+
+bool
+aarch64_expand_cmpstr (rtx *operands, bool bounded_p)
+{
+ return aarch64_expand_string_op (bounded_p ? BUILT_IN_STRNCMP
+ : BUILT_IN_STRCMP, operands);
+}
+
/* Split a DImode store of a CONST_INT SRC to MEM DST as two
SImode stores. Handle the case when the constant has identical
bottom and top halves. This is beneficial when the two stores can be
@@ -32416,7 +32702,6 @@ aarch64_invalid_binary_op (int op, const_tree type1,
return NULL;
}
-#define AARCH64_MEMTAG_GRANULE_SIZE 16
#define AARCH64_MEMTAG_TAG_BITSIZE 4
/* Implement TARGET_MEMTAG_TAG_BITSIZE. */
diff --git a/gcc/config/aarch64/aarch64.md b/gcc/config/aarch64/aarch64.md
index 9e7768e7fe1..059a15d9b60 100644
--- a/gcc/config/aarch64/aarch64.md
+++ b/gcc/config/aarch64/aarch64.md
@@ -2329,6 +2329,54 @@
FAIL;
})
+;; 0 is the result
+;; 1 is the memory to search
+;; 2 is the byte to search for
+;; 3 is the maximum number of bytes to search
+(define_expand "memchrdi"
+ [(match_operand:DI 0 "register_operand")
+ (match_operand:BLK 1 "memory_operand")
+ (match_operand:QI 2 "register_operand")
+ (match_operand:DI 3 "register_operand")]
+ ""
+{
+ if (aarch64_expand_memchr (operands))
+ DONE;
+ FAIL;
+})
+
+;; 0 is the result
+;; 1 and 2 are the strings to compare
+;; 3 is alignment
+(define_expand "cmpstrsi"
+ [(set (match_operand:SI 0 "register_operand")
+ (compare:SI (match_operand:BLK 1 "memory_operand")
+ (match_operand:BLK 2 "memory_operand")))
+ (use (match_operand 3 "immediate_operand"))]
+ ""
+{
+ if (aarch64_expand_cmpstr (operands, false))
+ DONE;
+ FAIL;
+})
+
+;; 0 is the result
+;; 1 and 2 are the strings to compare
+;; 3 is the maximum number of bytes to compare
+;; 4 is alignment
+(define_expand "cmpstrnsi"
+ [(set (match_operand:SI 0 "register_operand")
+ (compare:SI (match_operand:BLK 1 "memory_operand")
+ (match_operand:BLK 2 "memory_operand")))
+ (use (match_operand:DI 3 "register_operand"))
+ (use (match_operand 4 "immediate_operand"))]
+ ""
+{
+ if (aarch64_expand_cmpstr (operands, true))
+ DONE;
+ FAIL;
+})
+
(define_insn "*load_pair_<ldst_sz>"
[(set (match_operand:GPI 0 "aarch64_ldp_reg_operand")
(unspec [
diff --git a/gcc/config/aarch64/tuning_models/olympus.h
b/gcc/config/aarch64/tuning_models/olympus.h
index 1e701722e22..963b77fcf22 100644
--- a/gcc/config/aarch64/tuning_models/olympus.h
+++ b/gcc/config/aarch64/tuning_models/olympus.h
@@ -561,7 +561,8 @@ static struct tune_params olympus_tunings =
| AARCH64_EXTRA_TUNE_MATCHED_VECTOR_THROUGHPUT
| AARCH64_EXTRA_TUNE_AVOID_PRED_RMW
| AARCH64_EXTRA_TUNE_DISPATCH_SCHED
- | AARCH64_EXTRA_TUNE_NARROW_GP_WRITES), /* tune_flags. */
+ | AARCH64_EXTRA_TUNE_NARROW_GP_WRITES
+ | AARCH64_EXTRA_TUNE_INLINE_STRING_OPS), /* tune_flags. */
&olympus_prefetch_tune,
AARCH64_LDP_STP_POLICY_ALWAYS, /* ldp_policy_model. */
AARCH64_LDP_STP_POLICY_ALWAYS, /* stp_policy_model. */
diff --git a/gcc/testsuite/gcc.target/aarch64/inline-scan-1.c
b/gcc/testsuite/gcc.target/aarch64/inline-scan-1.c
new file mode 100644
index 00000000000..97b43a228e0
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/inline-scan-1.c
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=armv8.2-a+sve -mtune=olympus" } */
+
+/* memchr searches the first 16-byte granule inline and calls the library
+ function for the rest. */
+
+typedef __SIZE_TYPE__ size_t;
+
+const void *
+chr (const void *s, int c, size_t n)
+{
+ return __builtin_memchr (s, c, n);
+}
+
+/* { dg-final { scan-assembler-times {\twhilels\tp[0-9]+\.b, xzr, x[0-9]+}
+ 1 } } */
+/* { dg-final { scan-assembler-times {\tbl\tmemchr} 1 } } */
+/* { dg-final { scan-assembler-times {\tbrkb\t} 1 } } */
diff --git a/gcc/testsuite/gcc.target/aarch64/inline-scan-10.c
b/gcc/testsuite/gcc.target/aarch64/inline-scan-10.c
new file mode 100644
index 00000000000..12787d5d096
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/inline-scan-10.c
@@ -0,0 +1,21 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=armv8.2-a+sve -mtune=olympus -mstrict-align" } */
+
+/* The predicated byte accesses need no alignment, so strict alignment does
+ not change the string expansions. */
+
+typedef __SIZE_TYPE__ size_t;
+
+const void *
+chr (const void *s, int c, size_t n)
+{
+ return __builtin_memchr (s, c, n);
+}
+int cmp (const char *a) { return __builtin_strcmp (a, "abc"); }
+int
+ncmp (const char *a, const char *b, size_t n)
+{
+ return __builtin_strncmp (a, b, n);
+}
+
+/* { dg-final { scan-assembler-times {\twhilels\t} 3 } } */
diff --git a/gcc/testsuite/gcc.target/aarch64/inline-scan-11.c
b/gcc/testsuite/gcc.target/aarch64/inline-scan-11.c
new file mode 100644
index 00000000000..f1e1bccbbed
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/inline-scan-11.c
@@ -0,0 +1,20 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -mbig-endian -march=armv8.2-a+sve -mtune=olympus" } */
+
+/* The string expansions use byte accesses and work on big-endian targets. */
+
+typedef __SIZE_TYPE__ size_t;
+
+const void *
+chr (const void *s, int c, size_t n)
+{
+ return __builtin_memchr (s, c, n);
+}
+int cmp (const char *a) { return __builtin_strcmp (a, "abc"); }
+int
+ncmp (const char *a, const char *b, size_t n)
+{
+ return __builtin_strncmp (a, b, n);
+}
+
+/* { dg-final { scan-assembler-times {\twhilels\t} 3 } } */
diff --git a/gcc/testsuite/gcc.target/aarch64/inline-scan-12.c
b/gcc/testsuite/gcc.target/aarch64/inline-scan-12.c
new file mode 100644
index 00000000000..fbc2f694e39
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/inline-scan-12.c
@@ -0,0 +1,30 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=armv8.2-a+sve -mtune=olympus -fsanitize=thread" }
*/
+
+/* The thread sanitizer intercepts the string functions, so the library
+ calls remain. */
+
+typedef __SIZE_TYPE__ size_t;
+
+int
+cmp (const char *a, const char *b)
+{
+ return __builtin_strcmp (a, b);
+}
+
+int
+ncmp (const char *a, const char *b, size_t n)
+{
+ return __builtin_strncmp (a, b, n);
+}
+
+void *
+chr (const void *a, int c, size_t n)
+{
+ return __builtin_memchr (a, c, n);
+}
+
+/* { dg-final { scan-assembler-not {\twhilels\t} } } */
+/* { dg-final { scan-assembler {\tbl\tstrcmp} } } */
+/* { dg-final { scan-assembler {\tbl\tstrncmp} } } */
+/* { dg-final { scan-assembler {\tbl\tmemchr} } } */
diff --git a/gcc/testsuite/gcc.target/aarch64/inline-scan-13.c
b/gcc/testsuite/gcc.target/aarch64/inline-scan-13.c
new file mode 100644
index 00000000000..7638bb20c50
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/inline-scan-13.c
@@ -0,0 +1,30 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=armv8.2-a+sve+nosimd -mtune=olympus" } */
+
+/* +nosimd also disables SVE, so the string functions stay calls. */
+
+typedef __SIZE_TYPE__ size_t;
+
+const void *
+chr (const void *s, int c, size_t n)
+{
+ return __builtin_memchr (s, c, n);
+}
+
+int
+cmp (const char *a)
+{
+ return __builtin_strcmp (a, "abc");
+}
+
+int
+ncmp (const char *a, const char *b, size_t n)
+{
+ return __builtin_strncmp (a, b, n);
+}
+
+
+/* { dg-final { scan-assembler-not {\twhilels\t} } } */
+/* { dg-final { scan-assembler {\tb\tmemchr} } } */
+/* { dg-final { scan-assembler {\tb\tstrcmp} } } */
+/* { dg-final { scan-assembler {\tb\tstrncmp} } } */
diff --git a/gcc/testsuite/gcc.target/aarch64/inline-scan-14.c
b/gcc/testsuite/gcc.target/aarch64/inline-scan-14.c
new file mode 100644
index 00000000000..24e5d1f35a2
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/inline-scan-14.c
@@ -0,0 +1,30 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -mcpu=olympus+nosve" } */
+
+/* A CPU with the tuning flag but without SVE keeps the library calls. */
+
+typedef __SIZE_TYPE__ size_t;
+
+const void *
+chr (const void *s, int c, size_t n)
+{
+ return __builtin_memchr (s, c, n);
+}
+
+int
+cmp (const char *a)
+{
+ return __builtin_strcmp (a, "abc");
+}
+
+int
+ncmp (const char *a, const char *b, size_t n)
+{
+ return __builtin_strncmp (a, b, n);
+}
+
+
+/* { dg-final { scan-assembler-not {\twhilels\t} } } */
+/* { dg-final { scan-assembler {\tb\tmemchr} } } */
+/* { dg-final { scan-assembler {\tb\tstrcmp} } } */
+/* { dg-final { scan-assembler {\tb\tstrncmp} } } */
diff --git a/gcc/testsuite/gcc.target/aarch64/inline-scan-15.c
b/gcc/testsuite/gcc.target/aarch64/inline-scan-15.c
new file mode 100644
index 00000000000..5cda2722572
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/inline-scan-15.c
@@ -0,0 +1,33 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fsanitize=hwaddress -mtune=olympus" } */
+/* { dg-additional-options "-march=armv8.2-a+sve" } */
+
+/* The hardware-assisted address sanitizer does not disable the string
+ expansions. The inline loads are not instrumented, so only the bytes read
+ by the library calls are checked. */
+
+typedef __SIZE_TYPE__ size_t;
+
+const void *
+chr (const void *s, int c, size_t n)
+{
+ return __builtin_memchr (s, c, n);
+}
+
+int
+cmp (const char *a)
+{
+ return __builtin_strcmp (a, "abc");
+}
+
+int
+ncmp (const char *a, const char *b, size_t n)
+{
+ return __builtin_strncmp (a, b, n);
+}
+
+
+/* { dg-final { scan-assembler-times {\twhilels\t} 3 } } */
+/* { dg-final { scan-assembler {\tbl\tmemchr} } } */
+/* { dg-final { scan-assembler {\tbl\tstrcmp} } } */
+/* { dg-final { scan-assembler {\tbl\tstrncmp} } } */
diff --git a/gcc/testsuite/gcc.target/aarch64/inline-scan-2.c
b/gcc/testsuite/gcc.target/aarch64/inline-scan-2.c
new file mode 100644
index 00000000000..fbbca791e72
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/inline-scan-2.c
@@ -0,0 +1,30 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=armv8.2-a+sve -mtune=generic" } */
+
+/* Without a CPU that enables them, the string functions stay calls. */
+
+typedef __SIZE_TYPE__ size_t;
+
+const void *
+chr (const void *s, int c, size_t n)
+{
+ return __builtin_memchr (s, c, n);
+}
+
+int
+cmp (const char *a)
+{
+ return __builtin_strcmp (a, "abc");
+}
+
+int
+ncmp (const char *a, const char *b, size_t n)
+{
+ return __builtin_strncmp (a, b, n);
+}
+
+
+/* { dg-final { scan-assembler-times {\tb\tmemchr} 1 } } */
+/* { dg-final { scan-assembler-times {\tb\tstrcmp} 1 } } */
+/* { dg-final { scan-assembler-times {\tb\tstrncmp} 1 } } */
+/* { dg-final { scan-assembler-not {\twhilels\t} } } */
diff --git a/gcc/testsuite/gcc.target/aarch64/inline-scan-3.c
b/gcc/testsuite/gcc.target/aarch64/inline-scan-3.c
new file mode 100644
index 00000000000..9ac3d03b6ba
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/inline-scan-3.c
@@ -0,0 +1,22 @@
+/* { dg-do compile } */
+/* { dg-require-effective-target aarch64_mabi_ilp32 } */
+/* { dg-options "-O2 -mabi=ilp32 -mtune=olympus" } */
+/* { dg-additional-options "-march=armv8.2-a+sve -Wno-deprecated" } */
+
+/* The string expansions convert the library call results for ILP32. */
+
+typedef __SIZE_TYPE__ size_t;
+
+const void *
+chr (const void *s, int c, size_t n)
+{
+ return __builtin_memchr (s, c, n);
+}
+int cmp (const char *a) { return __builtin_strcmp (a, "abc"); }
+int
+ncmp (const char *a, const char *b, size_t n)
+{
+ return __builtin_strncmp (a, b, n);
+}
+
+/* { dg-final { scan-assembler-times {\twhilels\t} 3 } } */
diff --git a/gcc/testsuite/gcc.target/aarch64/inline-scan-4.c
b/gcc/testsuite/gcc.target/aarch64/inline-scan-4.c
new file mode 100644
index 00000000000..4f6119c6894
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/inline-scan-4.c
@@ -0,0 +1,31 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=armv9-a -mtune=olympus" } */
+
+/* Streaming code does not use the SVE string expansions. */
+
+#pragma GCC target "+sme"
+
+typedef __SIZE_TYPE__ size_t;
+
+const void *
+chr (const void *s, int c, size_t n) __arm_streaming
+{
+ return __builtin_memchr (s, c, n);
+}
+
+int
+cmp (const char *a) __arm_streaming
+{
+ return __builtin_strcmp (a, "abc");
+}
+
+int
+ncmp (const char *a, const char *b, size_t n) __arm_streaming
+{
+ return __builtin_strncmp (a, b, n);
+}
+
+/* { dg-final { scan-assembler-not {\twhilels\t} } } */
+/* { dg-final { scan-assembler {\tbl\tmemchr} } } */
+/* { dg-final { scan-assembler {\tbl\tstrcmp} } } */
+/* { dg-final { scan-assembler {\tbl\tstrncmp} } } */
diff --git a/gcc/testsuite/gcc.target/aarch64/inline-scan-5.c
b/gcc/testsuite/gcc.target/aarch64/inline-scan-5.c
new file mode 100644
index 00000000000..c7c3ed9376d
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/inline-scan-5.c
@@ -0,0 +1,31 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fsanitize=address -mtune=olympus" } */
+/* { dg-additional-options "-march=armv8.2-a+sve" } */
+
+/* The address sanitizer keeps the library calls, which it intercepts. */
+
+typedef __SIZE_TYPE__ size_t;
+
+const void *
+chr (const void *s, int c, size_t n)
+{
+ return __builtin_memchr (s, c, n);
+}
+
+int
+cmp (const char *a)
+{
+ return __builtin_strcmp (a, "abc");
+}
+
+int
+ncmp (const char *a, const char *b, size_t n)
+{
+ return __builtin_strncmp (a, b, n);
+}
+
+
+/* { dg-final { scan-assembler-not {\twhilels\t} } } */
+/* { dg-final { scan-assembler {\tb\tmemchr} } } */
+/* { dg-final { scan-assembler {\tb\tstrcmp} } } */
+/* { dg-final { scan-assembler {\tb\tstrncmp} } } */
diff --git a/gcc/testsuite/gcc.target/aarch64/inline-scan-6.c
b/gcc/testsuite/gcc.target/aarch64/inline-scan-6.c
new file mode 100644
index 00000000000..31395ccef13
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/inline-scan-6.c
@@ -0,0 +1,47 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=armv8.2-a+sve -mtune=olympus" } */
+
+/* The string expansions depend on whether the block is optimized for size.
+ A block that ends in a cold noreturn call and a cold function call the
+ library function, for strcmp, strncmp and memchr. */
+
+typedef __SIZE_TYPE__ size_t;
+
+extern void fail (int) __attribute__ ((noreturn, cold));
+
+int
+cmp_hot (const char *a)
+{
+ return __builtin_strcmp (a, "abc");
+}
+
+void
+cmp_on_error (const char *a, int bad)
+{
+ if (bad)
+ fail (__builtin_strcmp (a, "abc"));
+}
+
+__attribute__ ((cold)) int
+cmp_cold (const char *a)
+{
+ return __builtin_strcmp (a, "abc");
+}
+
+__attribute__ ((cold)) int
+ncmp_cold (const char *a, const char *b, size_t n)
+{
+ return __builtin_strncmp (a, b, n);
+}
+
+__attribute__ ((cold)) const void *
+chr_cold (const void *s, int c, size_t n)
+{
+ return __builtin_memchr (s, c, n);
+}
+
+/* { dg-final { scan-assembler-times {\twhilels\t} 1 } } */
+/* { dg-final { scan-assembler-times {\tbl\tstrcmp\n} 2 } } */
+/* { dg-final { scan-assembler-times {\tb\tstrcmp\n} 1 } } */
+/* { dg-final { scan-assembler-times {\tb\tstrncmp\n} 1 } } */
+/* { dg-final { scan-assembler-times {\tb\tmemchr\n} 1 } } */
diff --git a/gcc/testsuite/gcc.target/aarch64/inline-scan-7.c
b/gcc/testsuite/gcc.target/aarch64/inline-scan-7.c
new file mode 100644
index 00000000000..cdb6406294e
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/inline-scan-7.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O0 -march=armv8.2-a+sve -mtune=olympus" } */
+
+/* The string expansions are not used at -O0. */
+
+typedef __SIZE_TYPE__ size_t;
+
+void *chr (const void *s, int c, size_t n)
+{ return __builtin_memchr (s, c, n); }
+int cmp (const char *a, const char *b) { return __builtin_strcmp (a, b); }
+int ncmp (const char *a, const char *b, size_t n)
+{ return __builtin_strncmp (a, b, n); }
+
+/* { dg-final { scan-assembler-not {\twhilels\t} } } */
diff --git a/gcc/testsuite/gcc.target/aarch64/inline-scan-8.c
b/gcc/testsuite/gcc.target/aarch64/inline-scan-8.c
new file mode 100644
index 00000000000..d38ca2542bc
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/inline-scan-8.c
@@ -0,0 +1,20 @@
+/* { dg-do compile } */
+/* { dg-options "-Os -march=armv8.2-a+sve -mtune=olympus" } */
+
+/* The string expansions are not used at -Os. */
+
+typedef __SIZE_TYPE__ size_t;
+
+const void *
+chr (const void *s, int c, size_t n)
+{
+ return __builtin_memchr (s, c, n);
+}
+int cmp (const char *a, const char *b) { return __builtin_strcmp (a, b); }
+int
+ncmp (const char *a, const char *b, size_t n)
+{
+ return __builtin_strncmp (a, b, n);
+}
+
+/* { dg-final { scan-assembler-not {\twhilels\t} } } */
diff --git a/gcc/testsuite/gcc.target/aarch64/inline-scan-9.c
b/gcc/testsuite/gcc.target/aarch64/inline-scan-9.c
new file mode 100644
index 00000000000..e9f1f3814c1
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/inline-scan-9.c
@@ -0,0 +1,30 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -mgeneral-regs-only -mtune=olympus" } */
+
+/* Without SVE the string functions stay calls. */
+
+typedef __SIZE_TYPE__ size_t;
+
+const void *
+chr (const void *s, int c, size_t n)
+{
+ return __builtin_memchr (s, c, n);
+}
+
+int
+cmp (const char *a)
+{
+ return __builtin_strcmp (a, "abc");
+}
+
+int
+ncmp (const char *a, const char *b, size_t n)
+{
+ return __builtin_strncmp (a, b, n);
+}
+
+
+/* { dg-final { scan-assembler-not {\twhilels\t} } } */
+/* { dg-final { scan-assembler {\tb\tmemchr} } } */
+/* { dg-final { scan-assembler {\tb\tstrcmp} } } */
+/* { dg-final { scan-assembler {\tb\tstrncmp} } } */
diff --git a/gcc/testsuite/gcc.target/aarch64/inline-scan-run-1.c
b/gcc/testsuite/gcc.target/aarch64/inline-scan-run-1.c
new file mode 100644
index 00000000000..244bfff277f
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/inline-scan-run-1.c
@@ -0,0 +1,75 @@
+/* { dg-do run { target { *-*-linux* && aarch64_sve_hw } } } */
+/* { dg-options "-O2 -march=armv8.2-a+sve -mtune=olympus" } */
+
+/* Inline memchr must return the libc result for every start
+ offset, length and match position, and must not read outside the
+ 16-byte granules that the search reaches. */
+#include <stddef.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#define NOIPA __attribute__ ((noipa))
+NOIPA const void *
+chr_n (const void *s, int c, size_t n)
+{ return memchr (s, c, n); }
+NOIPA const void *chr_16 (const void *s, int c, size_t n)
+{ if (n > 16) __builtin_unreachable (); return memchr (s, c, n); }
+NOIPA const void *chr_1_8 (const void *s, int c, size_t n)
+{ if (n < 1 || n > 8) __builtin_unreachable (); return memchr (s, c, n); }
+
+static const unsigned char *
+ref_memchr (const unsigned char *s, int c, size_t n)
+{
+ for (size_t i = 0; i < n; i++)
+ if (s[i] == (unsigned char) c)
+ return s + i;
+ return NULL;
+}
+
+int
+main (void)
+{
+ size_t page = sysconf (_SC_PAGESIZE);
+ unsigned char *p = mmap (NULL, 3 * page, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ if (p == MAP_FAILED || mprotect (p, page, PROT_NONE) != 0
+ || mprotect (p + 2 * page, page, PROT_NONE) != 0)
+ abort ();
+ unsigned char *base = p + page;
+ static const int chars[] = { 'x', 0, 0xe9, -23, 0x1ff };
+
+ /* memchr near both ends of the page. */
+ for (int end = 0; end < 2; end++)
+ for (size_t off = 0; off < 48; off++)
+ for (size_t n = 0; n < 80; n++)
+ for (size_t pos = 0; pos <= n + 2; pos++)
+ for (unsigned ci = 0; ci < sizeof chars / sizeof chars[0]; ci++)
+ {
+ unsigned char *s = end ? base + page - n - (off & 15) : base +
off;
+ if (end && s + n > base + page)
+ continue;
+ int c = chars[ci];
+ memset (base, 'a', page);
+ if (pos < n)
+ s[pos] = (unsigned char) c;
+ const unsigned char *r = ref_memchr (s, c, n);
+ if (chr_n (s, c, n) != r)
+ abort ();
+ if (n <= 16 && chr_16 (s, c, n) != r)
+ abort ();
+ if (n >= 1 && n <= 8 && chr_1_8 (s, c, n) != r)
+ abort ();
+ }
+
+ /* memchr with a huge length finds a match. */
+ memset (base, 'a', page);
+ base[page - 1] = 'z';
+ for (size_t off = 0; off < 32; off++)
+ if (chr_n (base + off, 'z', SIZE_MAX) != base + page - 1)
+ abort ();
+
+ return 0;
+}
diff --git a/gcc/testsuite/gcc.target/aarch64/inline-scan-run-2.c
b/gcc/testsuite/gcc.target/aarch64/inline-scan-run-2.c
new file mode 100644
index 00000000000..854e6a43aff
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/inline-scan-run-2.c
@@ -0,0 +1,29 @@
+/* { dg-do run { target aarch64_sve_hw } } */
+/* { dg-options "-O2 -march=armv8.2-a+sve -mtune=olympus" } */
+
+/* The result of memchr can share a register with its first argument. */
+
+typedef __SIZE_TYPE__ size_t;
+
+__attribute__ ((noipa)) char *
+f (char *p, const char *cs, size_t n)
+{
+ for (; *cs; cs++)
+ {
+ p = __builtin_memchr (p, *cs, n);
+ if (!p)
+ break;
+ }
+ return p;
+}
+
+int
+main (void)
+{
+ static char buf[] = "abcdefghijklmnop";
+ if (f (buf, "cde", 20) != buf + 4)
+ __builtin_abort ();
+ if (f (buf, "cdz", 5) != 0)
+ __builtin_abort ();
+ return 0;
+}
diff --git a/gcc/testsuite/gcc.target/aarch64/inline-scan-run-3.c
b/gcc/testsuite/gcc.target/aarch64/inline-scan-run-3.c
new file mode 100644
index 00000000000..e00e047775c
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/inline-scan-run-3.c
@@ -0,0 +1,4 @@
+/* { dg-do run { target { *-*-linux* && aarch64_sve_hw } } } */
+/* { dg-options "-O2 -march=armv8.2-a+sve -mtune=olympus -mstrict-align" } */
+
+#include "inline-scan-run-1.c"
diff --git a/gcc/testsuite/gcc.target/aarch64/inline-strcmp-1.c
b/gcc/testsuite/gcc.target/aarch64/inline-strcmp-1.c
new file mode 100644
index 00000000000..4151e093583
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/inline-strcmp-1.c
@@ -0,0 +1,21 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=armv8.2-a+sve -mtune=olympus" } */
+
+/* strcmp with a string literal and strncmp compare the strings up to the
+ end of the first 16-byte granule of either string inline and call the
+ library function for the rest. */
+
+typedef __SIZE_TYPE__ size_t;
+
+int cmp (const char *a) { return __builtin_strcmp (a, "abc"); }
+int
+ncmp (const char *a, const char *b, size_t n)
+{
+ return __builtin_strncmp (a, b, n);
+}
+
+/* { dg-final { scan-assembler-times {\twhilels\tp[0-9]+\.b, xzr, x[0-9]+}
+ 2 } } */
+/* { dg-final { scan-assembler-times {\tcmpne\tp[0-9]+\.b} 2 } } */
+/* { dg-final { scan-assembler-times {\tbl\tstrcmp} 1 } } */
+/* { dg-final { scan-assembler-times {\tbl\tstrncmp} 1 } } */
diff --git a/gcc/testsuite/gcc.target/aarch64/inline-strcmp-2.c
b/gcc/testsuite/gcc.target/aarch64/inline-strcmp-2.c
new file mode 100644
index 00000000000..a03cb153a49
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/inline-strcmp-2.c
@@ -0,0 +1,22 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=armv8.2-a+sve -mtune=olympus" } */
+
+/* The first granule of two strings of unknown content often does not
+ contain the end of the comparison, so an unbounded strcmp is expanded
+ inline only if one string is a literal. strncmp is bounded and is
+ always expanded. */
+
+typedef __SIZE_TYPE__ size_t;
+
+int lit (const char *a) { return __builtin_strcmp (a, "abc"); }
+int nonlit (const char *a, const char *b) { return __builtin_strcmp (a, b); }
+int
+ncmp (const char *a, const char *b, size_t n)
+{
+ return __builtin_strncmp (a, b, n);
+}
+
+/* { dg-final { scan-assembler-times {\twhilels\t} 2 } } */
+/* { dg-final { scan-assembler-times {\tbl\tstrcmp} 1 } } */
+/* { dg-final { scan-assembler-times {\tb\tstrcmp} 1 } } */
+/* { dg-final { scan-assembler-times {\tbl\tstrncmp} 1 } } */
diff --git a/gcc/testsuite/gcc.target/aarch64/inline-strcmp-run-1.c
b/gcc/testsuite/gcc.target/aarch64/inline-strcmp-run-1.c
new file mode 100644
index 00000000000..7c5c9bcecbd
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/inline-strcmp-run-1.c
@@ -0,0 +1,135 @@
+/* { dg-do run { target { *-*-linux* && aarch64_sve_hw } } } */
+/* { dg-options "-O2 -march=armv8.2-a+sve -mtune=olympus" } */
+
+/* Inline strcmp with a string literal and strncmp must give the sign of the
+ libc result for every pair of start offsets, first difference, terminator
+ position and length, and must not read outside the 16-byte granules they
reach. */
+#include <stddef.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#define NOIPA __attribute__ ((noipa))
+NOIPA int cmp (const char *a, const char *b) { return strcmp (a, b); }
+NOIPA int
+ncmp (const char *a, const char *b, size_t n)
+{ return strncmp (a, b, n); }
+NOIPA int ncmp_8 (const char *a, const char *b, size_t n)
+{ if (n > 8) __builtin_unreachable (); return strncmp (a, b, n); }
+NOIPA int eq (const char *a, const char *b) { return strcmp (a, b) == 0; }
+#define LIT "abcdefghijklmnopqrstuvwxyz0123456789"
+NOIPA int lit (const char *a) { return strcmp (a, LIT); }
+NOIPA int lit_rev (const char *a) { return strcmp (LIT, a); }
+NOIPA int lit_eq (const char *a) { return strcmp (a, LIT) == 0; }
+
+static int
+sign (int x)
+{
+ return (x > 0) - (x < 0);
+}
+
+static int
+ref_ncmp (const unsigned char *a, const unsigned char *b, size_t n)
+{
+ for (size_t i = 0; i < n; i++)
+ {
+ if (a[i] != b[i])
+ return a[i] < b[i] ? -1 : 1;
+ if (!a[i])
+ return 0;
+ }
+ return 0;
+}
+
+static char *
+guarded (size_t page)
+{
+ char *p = mmap (NULL, 3 * page, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ if (p == MAP_FAILED || mprotect (p, page, PROT_NONE) != 0
+ || mprotect (p + 2 * page, page, PROT_NONE) != 0)
+ abort ();
+ return p + page;
+}
+
+int
+main (void)
+{
+ size_t page = sysconf (_SC_PAGESIZE);
+ char *pa = guarded (page), *pb = guarded (page);
+ for (size_t la = 0; la < 40; la++)
+ for (size_t extra = 0; extra < 3; extra++)
+ for (size_t pos = 0; pos <= la + 1; pos++)
+ for (int kind = 0; kind < 3; kind++)
+ {
+ /* Both strings end at the end of their page. Kind 2 makes the
+ second string EXTRA bytes longer. */
+ if (kind != 2 && extra != 0)
+ continue;
+ size_t lb = la + (kind == 2 ? extra : 0);
+ char *a = pa + page - 1 - la;
+ char *b = pb + page - 1 - lb;
+ memset (pa, 'q', page);
+ memset (pb, 'q', page);
+ pa[page - 1] = 0;
+ pb[page - 1] = 0;
+ if (pos < la && pos < lb)
+ b[pos] = kind == 0 ? 'r' : (char) 0xf1;
+ int expect = ref_ncmp ((unsigned char *) a, (unsigned char *) b,
+ SIZE_MAX);
+ if (sign (cmp (a, b)) != expect || eq (a, b) != (expect == 0))
+ abort ();
+ for (size_t n = 0; n < 50; n++)
+ if (sign (ncmp (a, b, n))
+ != ref_ncmp ((unsigned char *) a, (unsigned char *) b, n))
+ abort ();
+ for (size_t n = 0; n <= 8; n++)
+ if (sign (ncmp_8 (a, b, n))
+ != ref_ncmp ((unsigned char *) a, (unsigned char *) b, n))
+ abort ();
+ if (sign (ncmp (a, b, SIZE_MAX)) != expect)
+ abort ();
+ }
+ /* Different offsets within the granule. */
+ for (size_t oa = 0; oa < 16; oa++)
+ for (size_t ob = 0; ob < 16; ob++)
+ for (size_t len = 0; len < 40; len++)
+ for (size_t pos = 0; pos <= len; pos++)
+ {
+ char *a = pa + oa, *b = pb + ob;
+ memset (pa, 'q', 64);
+ memset (pb, 'q', 64);
+ a[len] = 0;
+ b[len] = 0;
+ if (pos < len)
+ b[pos] = 'a';
+ int expect = ref_ncmp ((unsigned char *) a, (unsigned char *) b,
+ SIZE_MAX);
+ if (sign (cmp (a, b)) != expect)
+ abort ();
+ if (sign (ncmp (a, b, len / 2 + 1))
+ != ref_ncmp ((unsigned char *) a, (unsigned char *) b,
+ len / 2 + 1))
+ abort ();
+ }
+ /* Strings compared with a literal, which end at the end of a page. */
+ for (size_t len = 0; len < 40; len++)
+ for (size_t pos = 0; pos <= len; pos++)
+ for (int kind = 0; kind < 2; kind++)
+ {
+ char *a = pa + page - 1 - len;
+ for (size_t i = 0; i < len; i++)
+ a[i] = LIT[i % (sizeof (LIT) - 1)];
+ a[len] = 0;
+ if (pos < len)
+ a[pos] = kind ? (char) 0xf1 : 'A';
+ int expect = ref_ncmp ((unsigned char *) a, (unsigned char *) LIT,
+ SIZE_MAX);
+ if (sign (lit (a)) != expect || sign (lit_rev (a)) != -expect
+ || lit_eq (a) != (expect == 0))
+ abort ();
+ }
+ return 0;
+}
diff --git a/gcc/testsuite/gcc.target/aarch64/inline-strcmp-run-2.c
b/gcc/testsuite/gcc.target/aarch64/inline-strcmp-run-2.c
new file mode 100644
index 00000000000..f5fc7f431de
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/inline-strcmp-run-2.c
@@ -0,0 +1,60 @@
+/* { dg-do run { target { *-*-linux* && aarch64_sve_hw } } } */
+/* { dg-options "-O2 -march=armv8.2-a+sve -mtune=olympus" } */
+
+/* strncmp of a constant string with a bound that is not constant. The
+ string is shorter than the bound or than the lower bound of its range. */
+#include <stddef.h>
+#include <string.h>
+
+#define NOIPA __attribute__ ((noipa))
+#define STR "abc"
+NOIPA int lit (const char *a, size_t n) { return strncmp (a, STR, n); }
+NOIPA int lit_rev (const char *a, size_t n) { return strncmp (STR, a, n); }
+NOIPA int lit_lo (const char *a, size_t n)
+{ if (n < 8 || n > 20) __builtin_unreachable (); return strncmp (a, STR, n); }
+NOIPA int lit_hi (const char *a, size_t n)
+{ if (n > 2) __builtin_unreachable (); return strncmp (a, STR, n); }
+
+static int
+sign (int x)
+{
+ return (x > 0) - (x < 0);
+}
+
+static int
+ref (const char *a, const char *b, size_t n)
+{
+ for (size_t i = 0; i < n; i++)
+ {
+ unsigned char x = a[i], y = b[i];
+ if (x != y)
+ return x < y ? -1 : 1;
+ if (!x)
+ return 0;
+ }
+ return 0;
+}
+
+int
+main (void)
+{
+ static const char *const s[]
+ = { "", "a", "ab", "abc", "abd", "abb", "abcd", "abc\xf1", "b",
+ "abcdefghijklmnopqrstuvwxyz" };
+ for (size_t i = 0; i < sizeof s / sizeof s[0]; i++)
+ {
+ for (size_t n = 0; n <= 40; n++)
+ {
+ if (sign (lit (s[i], n)) != ref (s[i], STR, n)
+ || sign (lit_rev (s[i], n)) != ref (STR, s[i], n))
+ __builtin_abort ();
+ if (n >= 8 && n <= 20 && sign (lit_lo (s[i], n)) != ref (s[i], STR,
n))
+ __builtin_abort ();
+ if (n <= 2 && sign (lit_hi (s[i], n)) != ref (s[i], STR, n))
+ __builtin_abort ();
+ }
+ if (sign (lit (s[i], (size_t) -1)) != ref (s[i], STR, 1000))
+ __builtin_abort ();
+ }
+ return 0;
+}
diff --git a/gcc/testsuite/gcc.target/aarch64/inline-strcmp-run-3.c
b/gcc/testsuite/gcc.target/aarch64/inline-strcmp-run-3.c
new file mode 100644
index 00000000000..543a38805eb
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/inline-strcmp-run-3.c
@@ -0,0 +1,4 @@
+/* { dg-do run { target { *-*-linux* && aarch64_sve_hw } } } */
+/* { dg-options "-O2 -march=armv8.2-a+sve -mtune=olympus -mstrict-align" } */
+
+#include "inline-strcmp-run-1.c"
--
2.54.0 (Apple Git-157)