https://gcc.gnu.org/g:a1d854e455efd12571bd2bfd35a676e17eaca793

commit r17-6351-ga1d854e455efd12571bd2bfd35a676e17eaca793
Author: Kyrylo Tkachov <[email protected]>
Date:   Mon Nov 16 12:05:35 2026 +0100

    middle-end: Do not use cmpstr and cmpstrn for block comparisons
    
    prepare_cmp_insn compares two BLKmode operands of a given size.  It tried
    cmpmem, then cmpstr, then cmpstrn.  cmpstr and cmpstrn stop at a null
    byte, so they do not compare the whole block, and a target that provides
    only these patterns would give a wrong result for a block with a null
    byte in it.
    
    The expander can also fail.  GEN_FCN then returns a null insn, which
    emit_insn ignores, so the result register is never set.
    
    This patch uses only cmpmem, and tries the next mode if the expander
    fails.
    
    Bootstrapped and tested on aarch64-none-linux-gnu.
    
    gcc/ChangeLog:
    
            * optabs.cc (prepare_cmp_insn): Use only cmpmem for BLKmode
            comparisons.  Try the next mode if the expander fails.
    
    Signed-off-by: Kyrylo Tkachov <[email protected]>

Diff:
---
 gcc/optabs.cc | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/gcc/optabs.cc b/gcc/optabs.cc
index 513b19bf26ff..ed64d6831e2f 100644
--- a/gcc/optabs.cc
+++ b/gcc/optabs.cc
@@ -4655,17 +4655,13 @@ prepare_cmp_insn (rtx x, rtx y, rtx *mask, enum 
rtx_code comparison, rtx size,
 
       gcc_assert (size);
 
-      /* Try to use a memory block compare insn - either cmpstr
-        or cmpmem will do.  */
+      /* Try to use a memory block compare insn.  Only cmpmem compares
+        the whole block.  cmpstr and cmpstrn stop at a null byte.  */
       opt_scalar_int_mode cmp_mode_iter;
       FOR_EACH_MODE_IN_CLASS (cmp_mode_iter, MODE_INT)
        {
          scalar_int_mode cmp_mode = cmp_mode_iter.require ();
          cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
-         if (cmp_code == CODE_FOR_nothing)
-           cmp_code = direct_optab_handler (cmpstr_optab, cmp_mode);
-         if (cmp_code == CODE_FOR_nothing)
-           cmp_code = direct_optab_handler (cmpstrn_optab, cmp_mode);
          if (cmp_code == CODE_FOR_nothing)
            continue;
 
@@ -4679,7 +4675,10 @@ prepare_cmp_insn (rtx x, rtx y, rtx *mask, enum rtx_code 
comparison, rtx size,
          result_mode = insn_data[cmp_code].operand[0].mode;
          result = gen_reg_rtx (result_mode);
          size = convert_to_mode (cmp_mode, size, 1);
-         emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
+         rtx_insn *insn = GEN_FCN (cmp_code) (result, x, y, size, opalign);
+         if (!insn)
+           continue;
+         emit_insn (insn);
 
           *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
           *pmode = result_mode;

Reply via email to