From: Kyrylo Tkachov <[email protected]>

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.

This patch gives another 0.5% on top of the previous patches on Snappy
compress performance on NVIDIA Vera (taking the total gain  from the
series to about 5%)
Bootstrapped and tested on aarch64-none-linux-gnu.
Ok for trunk?
Thanks,
Kyrill

Cc: Tamar Christina <[email protected]>

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]>
---
 gcc/config/aarch64/aarch64.cc                 | 66 +++++++++++--------
 gcc/testsuite/gcc.target/aarch64/pr123017_8.c | 59 +++++++++++++++++
 2 files changed, 98 insertions(+), 27 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/aarch64/pr123017_8.c

diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc
index 282be9a9b1c..7cc741074a8 100644
--- a/gcc/config/aarch64/aarch64.cc
+++ b/gcc/config/aarch64/aarch64.cc
@@ -2426,34 +2426,46 @@ 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))
-    {
-      rtx set = single_set (insn);
-      if (!set)
-       continue;
-
-      rtx src = SET_SRC (set);
-      rtx dst = SET_DEST (set);
-      machine_mode mode = GET_MODE (src);
-      if (GET_MODE_CLASS (mode) != MODE_INT)
-       continue;
+  /* 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 inc = NULL_RTX, sel = NULL_RTX;
+      unsigned int nsets = 0;
+      for (rtx_insn *insn = seq; insn; insn = NEXT_INSN (insn))
+       {
+         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;
 
-      switch (GET_CODE (src))
-       {
-       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;
-       }
+         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);
+       }
+
+      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 00000000000..da7596946c8
--- /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 } } */
-- 
2.54.0 (Apple Git-157)

Reply via email to