https://gcc.gnu.org/g:48052d4ee1f912805b489bcd6f115bffa0bc29cc

commit r17-6586-g48052d4ee1f912805b489bcd6f115bffa0bc29cc
Author: Kyrylo Tkachov <[email protected]>
Date:   Wed Oct 21 09:39:13 2026 +0200

    AArch64: Limit the CINC exception in the if-conversion cost check
    
    aarch64_noce_conversion_profitable_p skips its cost check when any
    instruction of the sequence adds or subtracts one from a register,
    because the select is then likely a CSINC.  It looks at every
    instruction, so a long sequence that happens to contain an increment is
    accepted however long it is.  In
    
      for (int i = 0; i < n; i++)
        if (a[i] > lim)
          {
            cnt++;
            s += a[i] * 3;
            t ^= a[i] * 5;
          }
    
    the counter makes the whole multiple-set sequence pass, and the loop then
    computes all three updates on every iteration, even when the arm is
    rarely taken.
    
    Skip the cost check only when, apart from comparisons, the sequence is
    one increment and one conditional select that uses it, which is the shape
    that becomes a single CSINC.  Other sequences go through the cost check.
    
    This relies on the parallel cost estimate of the previous patch.  Under
    the serial cost, the multiple-set conversions that
    ifcvt_multiple_sets_arithm.c and ifcvt_multiple_sets_diamond_{2,5}.c
    expect fail the cost check and pass only through this exception.
    
    Over the C torture and aarch64 target tests compiled at -O2, -O3 and
    -Os (13497 outputs), 8 outputs change: 6 lose instructions and 2 keep
    the same count, for 12 fewer instructions overall.
    
    Snappy (rev 26aa88cb) was built at -O3 -mcpu=olympus and run on an
    NVIDIA Vera system, six rounds in alternating order on one pinned core.
    The whole series raises compression (ZFlat) throughput by 5.1% geomean
    against 4.7% without this patch, and decompression (UFlat) by 0.1%.  The
    difference is within the variation between runs, so this patch keeps
    the Snappy gain.
    
    gcc/ChangeLog:
    
            * config/aarch64/aarch64.cc (aarch64_noce_conversion_profitable_p):
            Skip the cost check for an increment only if the sequence is that
            increment and one conditional select that uses it.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.target/aarch64/pr123017_8.c: New test.
    
    Signed-off-by: Kyrylo Tkachov <[email protected]>

Diff:
---
 gcc/config/aarch64/aarch64.cc                 | 74 ++++++++++++++++++---------
 gcc/testsuite/gcc.target/aarch64/pr123017_8.c | 59 +++++++++++++++++++++
 2 files changed, 109 insertions(+), 24 deletions(-)

diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc
index 016af1486592..17b3fc730504 100644
--- a/gcc/config/aarch64/aarch64.cc
+++ b/gcc/config/aarch64/aarch64.cc
@@ -2426,34 +2426,60 @@ aarch64_noce_conversion_profitable_p (rtx_insn *seq,
       && !SUBREG_P (if_info->b))
     is_csel_p = false;
 
-  for (rtx_insn *insn = seq; is_csel_p && insn; insn = NEXT_INSN (insn))
+  /* A CSINC selects between a register and another register plus or minus
+     one, so an increment that feeds a select costs no extra instruction.
+     Treat such a sequence like the cases above, but only if the increment
+     and the select are all it computes apart from comparisons.  An
+     increment elsewhere in a longer sequence says nothing about the rest of
+     it.  */
+  if (is_csel_p)
     {
-      rtx set = single_set (insn);
-      if (!set)
-       continue;
+      /* INC is the destination of a set that adds or subtracts one from a
+        register, such as R101 in
 
-      rtx src = SET_SRC (set);
-      rtx dst = SET_DEST (set);
-      machine_mode mode = GET_MODE (src);
-      if (GET_MODE_CLASS (mode) != MODE_INT)
-       continue;
+          (set (reg:SI 101) (plus:SI (reg:SI 100) (const_int 1)))
+
+        SEL is the source of a conditional select, such as
+
+          (if_then_else:SI (gt (reg:CC 66) (const_int 0))
+                           (reg:SI 101) (reg:SI 100))
 
-      switch (GET_CODE (src))
+        NSETS counts the instructions that do not set a condition-code
+        register.  */
+      rtx inc = NULL_RTX, sel = NULL_RTX;
+      unsigned int nsets = 0;
+      for (rtx_insn *insn = seq; insn; insn = NEXT_INSN (insn))
        {
-       case PLUS:
-       case MINUS:
-         {
-           /* Likely a CINC.  */
-           if (REG_P (dst)
-               && (REG_P (XEXP (src, 0)) || SUBREG_P (XEXP (src, 0)))
-               && (XEXP (src, 1) == CONST1_RTX (mode)
-                   || XEXP (src, 1) == CONSTM1_RTX (mode)))
-             is_csel_p = false;
-           break;
-         }
-       default:
-         break;
-       }
+         if (!NONDEBUG_INSN_P (insn))
+           continue;
+         rtx set = single_set (insn);
+         if (set && GET_MODE_CLASS (GET_MODE (SET_DEST (set))) == MODE_CC)
+           continue;
+         nsets++;
+         if (!set)
+           continue;
+
+         rtx src = SET_SRC (set);
+         machine_mode mode = GET_MODE (src);
+         if (GET_CODE (src) == IF_THEN_ELSE)
+           sel = src;
+         else if ((GET_CODE (src) == PLUS || GET_CODE (src) == MINUS)
+                  && GET_MODE_CLASS (mode) == MODE_INT
+                  && REG_P (SET_DEST (set))
+                  && (REG_P (XEXP (src, 0)) || SUBREG_P (XEXP (src, 0)))
+                  && (XEXP (src, 1) == CONST1_RTX (mode)
+                      || XEXP (src, 1) == CONSTM1_RTX (mode)))
+           inc = SET_DEST (set);
+       }
+
+      /* The sequence becomes a single CSINC if it is only INC and SEL, and
+        one arm of SEL is INC.  */
+      if (nsets == 2
+         && inc
+         && sel
+         && (rtx_equal_p (XEXP (sel, 1), inc)
+             || rtx_equal_p (XEXP (sel, 2), inc)))
+       is_csel_p = false;
     }
 
   /* For now accept every variant but csel unconditionally because CSEL usually
diff --git a/gcc/testsuite/gcc.target/aarch64/pr123017_8.c 
b/gcc/testsuite/gcc.target/aarch64/pr123017_8.c
new file mode 100644
index 000000000000..da7596946c83
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/pr123017_8.c
@@ -0,0 +1,59 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+/* An increment inside a longer arm does not make the select a CSINC, so
+   the cost limit still applies.  */
+
+void
+chain (const int *restrict in, int *restrict out, int n, int threshold)
+{
+  for (int i = 0; i < n; ++i)
+    {
+      int v = in[i];
+      if (v > threshold)
+       {
+         int t = v * 3;
+         t += 1;
+         t ^= 0x55;
+         t *= 0x55;
+         t -= 0x5;
+         t &= 0xFE;
+         t ^= 0x55;
+         out[i] = t;
+       }
+      else
+       out[i] = v;
+    }
+}
+
+int
+multi (const int *a, int n, int lim)
+{
+  int cnt = 0, s = 0, t = 0;
+  for (int i = 0; i < n; i++)
+    {
+      int v = a[i];
+      if (v > lim)
+       {
+         cnt++;
+         s += v * 3;
+         t ^= v * 5;
+       }
+    }
+  return cnt + s + t;
+}
+
+/* A lone conditional increment is still converted.  */
+
+int
+count (const int *a, int n, int lim)
+{
+  int cnt = 0;
+  for (int i = 0; i < n; i++)
+    if (a[i] > lim)
+      cnt++;
+  return cnt;
+}
+
+/* { dg-final { scan-assembler-not "csel\t" } } */
+/* { dg-final { scan-assembler-times "cinc\t" 1 } } */

Reply via email to