public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-7249] aarch64: Fix subs_compare_2.c regression [PR100874]
@ 2022-02-15 18:10 Richard Sandiford
  0 siblings, 0 replies; only message in thread
From: Richard Sandiford @ 2022-02-15 18:10 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:8e84b2b37a541b27feea69769fc314d534464ebd

commit r12-7249-g8e84b2b37a541b27feea69769fc314d534464ebd
Author: Richard Sandiford <richard.sandiford@arm.com>
Date:   Tue Feb 15 18:09:35 2022 +0000

    aarch64: Fix subs_compare_2.c regression [PR100874]
    
    subs_compare_2.c tests that we can use a SUBS+CSEL sequence for:
    
    unsigned int
    foo (unsigned int a, unsigned int b)
    {
      unsigned int x = a - 4;
      if (a < 4)
        return x;
      else
        return 0;
    }
    
    As Andrew notes in the PR, this is effectively MIN (x, 4) - 4,
    and it is now recognised as such by phiopt.  Previously it was
    if-converted in RTL instead.
    
    I tried to look for ways to generalise this to other situations
    and to other ?:-style operations, not just max and min.  However,
    for general ?: we tend to push an outer “- CST” into the arms of
    the ?: -- at least if one of them simplifies -- so I didn't find
    any useful abstraction.
    
    This patch therefore adds a pattern specifically for
    max/min(a,cst)-cst.  I'm not thrilled at having to do this,
    but it seems like the least worst fix in the circumstances.
    Also, max(a,cst)-cst for unsigned a is a useful saturating
    subtraction idiom and so is arguably worth its own code
    for that reason.
    
    gcc/
            PR target/100874
            * config/aarch64/aarch64-protos.h (aarch64_maxmin_plus_const):
            Declare.
            * config/aarch64/aarch64.cc (aarch64_maxmin_plus_const): New function.
            * config/aarch64/aarch64.md (*aarch64_minmax_plus): New pattern.
    
    gcc/testsuite/
            * gcc.target/aarch64/max_plus_1.c: New test.
            * gcc.target/aarch64/max_plus_2.c: Likewise.
            * gcc.target/aarch64/max_plus_3.c: Likewise.
            * gcc.target/aarch64/max_plus_4.c: Likewise.
            * gcc.target/aarch64/max_plus_5.c: Likewise.
            * gcc.target/aarch64/max_plus_6.c: Likewise.
            * gcc.target/aarch64/max_plus_7.c: Likewise.
            * gcc.target/aarch64/min_plus_1.c: Likewise.
            * gcc.target/aarch64/min_plus_2.c: Likewise.
            * gcc.target/aarch64/min_plus_3.c: Likewise.
            * gcc.target/aarch64/min_plus_4.c: Likewise.
            * gcc.target/aarch64/min_plus_5.c: Likewise.
            * gcc.target/aarch64/min_plus_6.c: Likewise.
            * gcc.target/aarch64/min_plus_7.c: Likewise.

Diff:
---
 gcc/config/aarch64/aarch64-protos.h           |   1 +
 gcc/config/aarch64/aarch64.cc                 | 104 ++++++++++++++++++
 gcc/config/aarch64/aarch64.md                 |  27 +++++
 gcc/testsuite/gcc.target/aarch64/max_plus_1.c | 149 ++++++++++++++++++++++++++
 gcc/testsuite/gcc.target/aarch64/max_plus_2.c |  35 ++++++
 gcc/testsuite/gcc.target/aarch64/max_plus_3.c |  35 ++++++
 gcc/testsuite/gcc.target/aarch64/max_plus_4.c |  30 ++++++
 gcc/testsuite/gcc.target/aarch64/max_plus_5.c |  35 ++++++
 gcc/testsuite/gcc.target/aarch64/max_plus_6.c |   9 ++
 gcc/testsuite/gcc.target/aarch64/max_plus_7.c |  35 ++++++
 gcc/testsuite/gcc.target/aarch64/min_plus_1.c | 149 ++++++++++++++++++++++++++
 gcc/testsuite/gcc.target/aarch64/min_plus_2.c |  35 ++++++
 gcc/testsuite/gcc.target/aarch64/min_plus_3.c |  35 ++++++
 gcc/testsuite/gcc.target/aarch64/min_plus_4.c |  30 ++++++
 gcc/testsuite/gcc.target/aarch64/min_plus_5.c |  35 ++++++
 gcc/testsuite/gcc.target/aarch64/min_plus_6.c |   9 ++
 gcc/testsuite/gcc.target/aarch64/min_plus_7.c |  35 ++++++
 17 files changed, 788 insertions(+)

diff --git a/gcc/config/aarch64/aarch64-protos.h b/gcc/config/aarch64/aarch64-protos.h
index 392efa0b74d..d0e78d6a559 100644
--- a/gcc/config/aarch64/aarch64-protos.h
+++ b/gcc/config/aarch64/aarch64-protos.h
@@ -939,6 +939,7 @@ bool aarch64_legitimate_address_p (machine_mode, rtx, bool,
 				   aarch64_addr_query_type = ADDR_QUERY_M);
 machine_mode aarch64_select_cc_mode (RTX_CODE, rtx, rtx);
 rtx aarch64_gen_compare_reg (RTX_CODE, rtx, rtx);
+bool aarch64_maxmin_plus_const (rtx_code, rtx *, bool);
 rtx aarch64_load_tp (rtx);
 
 void aarch64_expand_compare_and_swap (rtx op[]);
diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc
index 1a460d4bac0..37ed22bcc94 100644
--- a/gcc/config/aarch64/aarch64.cc
+++ b/gcc/config/aarch64/aarch64.cc
@@ -3781,6 +3781,110 @@ aarch64_gen_compare_reg_maybe_ze (RTX_CODE code, rtx x, rtx y,
   return aarch64_gen_compare_reg (code, x, y);
 }
 
+/* Consider the operation:
+
+     OPERANDS[0] = CODE (OPERANDS[1], OPERANDS[2]) + OPERANDS[3]
+
+   where:
+
+   - CODE is [SU]MAX or [SU]MIN
+   - OPERANDS[2] and OPERANDS[3] are constant integers
+   - OPERANDS[3] is a positive or negative shifted 12-bit immediate
+   - all operands have mode MODE
+
+   Decide whether it is possible to implement the operation using:
+
+     SUBS <tmp>, OPERANDS[1], -OPERANDS[3]
+     or
+     ADDS <tmp>, OPERANDS[1], OPERANDS[3]
+
+   followed by:
+
+     <insn> OPERANDS[0], <tmp>, [wx]zr, <cond>
+
+   where <insn> is one of CSEL, CSINV or CSINC.  Return true if so.
+   If GENERATE_P is true, also update OPERANDS as follows:
+
+     OPERANDS[4] = -OPERANDS[3]
+     OPERANDS[5] = the rtl condition representing <cond>
+     OPERANDS[6] = <tmp>
+     OPERANDS[7] = 0 for CSEL, -1 for CSINV or 1 for CSINC.  */
+bool
+aarch64_maxmin_plus_const (rtx_code code, rtx *operands, bool generate_p)
+{
+  signop sgn = (code == UMAX || code == UMIN ? UNSIGNED : SIGNED);
+  rtx dst = operands[0];
+  rtx maxmin_op = operands[2];
+  rtx add_op = operands[3];
+  machine_mode mode = GET_MODE (dst);
+
+  /* max (x, y) - z == (x >= y + 1 ? x : y) - z
+		    == (x >= y ? x : y) - z
+		    == (x > y ? x : y) - z
+		    == (x > y - 1 ? x : y) - z
+
+     min (x, y) - z == (x <= y - 1 ? x : y) - z
+		    == (x <= y ? x : y) - z
+		    == (x < y ? x : y) - z
+		    == (x < y + 1 ? x : y) - z
+
+     Check whether z is in { y - 1, y, y + 1 } and pick the form(s) for
+     which x is compared with z.  Set DIFF to y - z.  Thus the supported
+     combinations are as follows, with DIFF being the value after the ":":
+
+     max (x, y) - z == x >= y + 1 ? x - (y + 1) : -1   [z == y + 1]
+		    == x >= y ? x - y : 0              [z == y]
+		    == x > y ? x - y : 0               [z == y]
+		    == x > y - 1 ? x - (y - 1) : 1     [z == y - 1]
+
+     min (x, y) - z == x <= y - 1 ? x - (y - 1) : 1    [z == y - 1]
+		    == x <= y ? x - y : 0              [z == y]
+		    == x < y ? x - y : 0               [z == y]
+		    == x < y + 1 ? x - (y + 1) : -1    [z == y + 1].  */
+  auto maxmin_val = rtx_mode_t (maxmin_op, mode);
+  auto add_val = rtx_mode_t (add_op, mode);
+  auto sub_val = wi::neg (add_val);
+  auto diff = wi::sub (maxmin_val, sub_val);
+  if (!(diff == 0
+	|| (diff == 1 && wi::gt_p (maxmin_val, sub_val, sgn))
+	|| (diff == -1 && wi::lt_p (maxmin_val, sub_val, sgn))))
+    return false;
+
+  if (!generate_p)
+    return true;
+
+  rtx_code cmp;
+  switch (code)
+    {
+    case SMAX:
+      cmp = diff == 1 ? GT : GE;
+      break;
+    case UMAX:
+      cmp = diff == 1 ? GTU : GEU;
+      break;
+    case SMIN:
+      cmp = diff == -1 ? LT : LE;
+      break;
+    case UMIN:
+      cmp = diff == -1 ? LTU : LEU;
+      break;
+    default:
+      gcc_unreachable ();
+    }
+  rtx cc = gen_rtx_REG (CCmode, CC_REGNUM);
+
+  operands[4] = immed_wide_int_const (sub_val, mode);
+  operands[5] = gen_rtx_fmt_ee (cmp, VOIDmode, cc, const0_rtx);
+  if (can_create_pseudo_p ())
+    operands[6] = gen_reg_rtx (mode);
+  else
+    operands[6] = dst;
+  operands[7] = immed_wide_int_const (diff, mode);
+
+  return true;
+}
+
+
 /* Build the SYMBOL_REF for __tls_get_addr.  */
 
 static GTY(()) rtx tls_get_addr_libfunc;
diff --git a/gcc/config/aarch64/aarch64.md b/gcc/config/aarch64/aarch64.md
index 3c72bdad01b..64cc21d5802 100644
--- a/gcc/config/aarch64/aarch64.md
+++ b/gcc/config/aarch64/aarch64.md
@@ -4405,6 +4405,33 @@
   }
 )
 
+;; Implement MAX/MIN (A, B) - C using SUBS/ADDS followed by CSEL/CSINV/CSINC.
+;; See aarch64_maxmin_plus_const for details about the supported cases.
+(define_insn_and_split "*aarch64_minmax_plus"
+  [(set (match_operand:GPI 0 "register_operand" "=r")
+	(plus:GPI
+	  (MAXMIN:GPI
+	    (match_operand:GPI 1 "register_operand" "r")
+	    (match_operand:GPI 2 "const_int_operand"))
+	  (match_operand:GPI 3 "aarch64_plus_immediate")))
+   (clobber (reg:CC CC_REGNUM))]
+  "aarch64_maxmin_plus_const (<CODE>, operands, false)"
+  "#"
+  "&& 1"
+  [(parallel
+     [(set (reg:CC CC_REGNUM)
+	   (compare:CC (match_dup 1) (match_dup 4)))
+      (set (match_dup 6)
+	   (plus:GPI (match_dup 1) (match_dup 3)))])
+   (set (match_dup 0)
+	(if_then_else:GPI (match_dup 5) (match_dup 6) (match_dup 7)))]
+  {
+    if (!aarch64_maxmin_plus_const (<CODE>, operands, true))
+      gcc_unreachable ();
+  }
+  [(set_attr "length" "8")]
+)
+
 ;; -------------------------------------------------------------------
 ;; Logical operations
 ;; -------------------------------------------------------------------
diff --git a/gcc/testsuite/gcc.target/aarch64/max_plus_1.c b/gcc/testsuite/gcc.target/aarch64/max_plus_1.c
new file mode 100644
index 00000000000..ef336aeec4f
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/max_plus_1.c
@@ -0,0 +1,149 @@
+/* { dg-do run } */
+/* { dg-options "-O2 --save-temps" } */
+/* { dg-final { check-function-bodies "**" "" "" } } */
+
+/*
+** f1:
+**	adds	(w[0-9]+), w0, #4
+**	csel	w0, \1, wzr, g[te]
+**	ret
+*/
+/*
+** f2:
+**	adds	(w[0-9]+), w0, #4
+**	csel	w0, \1, wzr, g[te]
+**	ret
+*/
+/*
+** f3:
+**	adds	(w[0-9]+), w0, #5
+**	csinc	w0, \1, wzr, gt
+**	ret
+*/
+/*
+** f4:
+**	adds	(w[0-9]+), w0, #3
+**	csinv	w0, \1, wzr, ge
+**	ret
+*/
+
+#ifndef TYPE
+#define TYPE int32_t
+#define TYPE_MIN INT32_MIN
+#define TYPE_MAX INT32_MAX
+#define VALUE -4
+#endif
+
+#include <stdint.h>
+
+TYPE __attribute__((noipa))
+f1 (TYPE x)
+{
+  return (x > VALUE ? x - VALUE : 0);
+}
+
+TYPE __attribute__((noipa))
+f2 (TYPE x)
+{
+  return (x > VALUE ? x : VALUE) - VALUE;
+}
+
+TYPE __attribute__((noipa))
+f3 (TYPE x)
+{
+  return (x > VALUE ? x : VALUE) - (VALUE - 1);
+}
+
+TYPE __attribute__((noipa))
+f4 (TYPE x)
+{
+  return (x > VALUE ? x : VALUE) - (VALUE + 1);
+}
+
+TYPE __attribute__((noipa))
+f5 (TYPE x)
+{
+  return (x > VALUE ? x : VALUE) - (VALUE + 2);
+}
+
+TYPE __attribute__((noipa))
+f6 (TYPE x)
+{
+  return (x > VALUE ? x : VALUE) - (VALUE - 2);
+}
+
+int
+main (void)
+{
+  TYPE max_test = TYPE_MAX;
+  if (TYPE_MIN < 0 && VALUE < 0)
+    max_test += VALUE;
+
+  if (f1 (TYPE_MIN) != 0)
+    __builtin_abort ();
+  if (f1 (VALUE - 1) != 0)
+    __builtin_abort ();
+  if (f1 (VALUE) != 0)
+    __builtin_abort ();
+  if (f1 (VALUE + 1) != 1)
+    __builtin_abort ();
+  if (f1 (max_test) != max_test - VALUE)
+    __builtin_abort ();
+
+  if (f2 (TYPE_MIN) != 0)
+    __builtin_abort ();
+  if (f2 (VALUE - 1) != 0)
+    __builtin_abort ();
+  if (f2 (VALUE) != 0)
+    __builtin_abort ();
+  if (f2 (VALUE + 1) != 1)
+    __builtin_abort ();
+  if (f2 (max_test) != max_test - VALUE)
+    __builtin_abort ();
+
+  if (f3 (TYPE_MIN) != 1)
+    __builtin_abort ();
+  if (f3 (VALUE - 1) != 1)
+    __builtin_abort ();
+  if (f3 (VALUE) != 1)
+    __builtin_abort ();
+  if (f3 (VALUE + 1) != 2)
+    __builtin_abort ();
+  if (f3 (max_test - 1) != max_test - VALUE)
+    __builtin_abort ();
+
+  if (f4 (TYPE_MIN) != -1)
+    __builtin_abort ();
+  if (f4 (VALUE - 1) != -1)
+    __builtin_abort ();
+  if (f4 (VALUE) != -1)
+    __builtin_abort ();
+  if (f4 (VALUE + 1) != 0)
+    __builtin_abort ();
+  if (f4 (max_test) != max_test - VALUE - 1)
+    __builtin_abort ();
+
+  if (f5 (TYPE_MIN) != -2)
+    __builtin_abort ();
+  if (f5 (VALUE - 1) != -2)
+    __builtin_abort ();
+  if (f5 (VALUE) != -2)
+    __builtin_abort ();
+  if (f5 (VALUE + 1) != -1)
+    __builtin_abort ();
+  if (f5 (max_test) != max_test - VALUE - 2)
+    __builtin_abort ();
+
+  if (f6 (TYPE_MIN) != 2)
+    __builtin_abort ();
+  if (f6 (VALUE - 1) != 2)
+    __builtin_abort ();
+  if (f6 (VALUE) != 2)
+    __builtin_abort ();
+  if (f6 (VALUE + 1) != 3)
+    __builtin_abort ();
+  if (VALUE <= max_test - 2 && f6 (max_test - 2) != max_test - VALUE)
+    __builtin_abort ();
+
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.target/aarch64/max_plus_2.c b/gcc/testsuite/gcc.target/aarch64/max_plus_2.c
new file mode 100644
index 00000000000..a2a1295d918
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/max_plus_2.c
@@ -0,0 +1,35 @@
+/* { dg-do run } */
+/* { dg-options "-O2 --save-temps" } */
+/* { dg-final { check-function-bodies "**" "" "" } } */
+
+/*
+** f1:
+**	adds	(x[0-9]+), x0, #4094
+**	csel	x0, \1, xzr, g[te]
+**	ret
+*/
+/*
+** f2:
+**	adds	(x[0-9]+), x0, #4094
+**	csel	x0, \1, xzr, g[te]
+**	ret
+*/
+/*
+** f3:
+**	adds	(x[0-9]+), x0, #4095
+**	csinc	x0, \1, xzr, gt
+**	ret
+*/
+/*
+** f4:
+**	adds	(x[0-9]+), x0, #4093
+**	csinv	x0, \1, xzr, ge
+**	ret
+*/
+
+#define TYPE int64_t
+#define TYPE_MIN INT64_MIN
+#define TYPE_MAX INT64_MAX
+#define VALUE -4094
+
+#include "max_plus_1.c"
diff --git a/gcc/testsuite/gcc.target/aarch64/max_plus_3.c b/gcc/testsuite/gcc.target/aarch64/max_plus_3.c
new file mode 100644
index 00000000000..a9792ecc94d
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/max_plus_3.c
@@ -0,0 +1,35 @@
+/* { dg-do run } */
+/* { dg-options "-O2 --save-temps" } */
+/* { dg-final { check-function-bodies "**" "" "" } } */
+
+/*
+** f1:
+**	adds	(w[0-9]+), w0, #4095
+**	csel	w0, \1, wzr, g[te]
+**	ret
+*/
+/*
+** f2:
+**	adds	(w[0-9]+), w0, #4095
+**	csel	w0, \1, wzr, g[te]
+**	ret
+*/
+/*
+** f3:
+**	adds	(w[0-9]+), w0, #4096
+**	csinc	w0, \1, wzr, gt
+**	ret
+*/
+/*
+** f4:
+**	adds	(w[0-9]+), w0, #4094
+**	csinv	w0, \1, wzr, ge
+**	ret
+*/
+
+#define TYPE int32_t
+#define TYPE_MIN INT32_MIN
+#define TYPE_MAX INT32_MAX
+#define VALUE -4095
+
+#include "max_plus_1.c"
diff --git a/gcc/testsuite/gcc.target/aarch64/max_plus_4.c b/gcc/testsuite/gcc.target/aarch64/max_plus_4.c
new file mode 100644
index 00000000000..5090fa10192
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/max_plus_4.c
@@ -0,0 +1,30 @@
+/* { dg-do run } */
+/* { dg-options "-O2 --save-temps" } */
+/* { dg-final { check-function-bodies "**" "" "" } } */
+
+/*
+** f1:
+**	adds	(x[0-9]+), x0, #4096
+**	csel	x0, \1, xzr, g[te]
+**	ret
+*/
+/*
+** f2:
+**	adds	(x[0-9]+), x0, #4096
+**	csel	x0, \1, xzr, g[te]
+**	ret
+*/
+/* f3 out of range */
+/*
+** f4:
+**	adds	(x[0-9]+), x0, #4095
+**	csinv	x0, \1, xzr, ge
+**	ret
+*/
+
+#define TYPE int64_t
+#define TYPE_MIN INT64_MIN
+#define TYPE_MAX INT64_MAX
+#define VALUE -4096
+
+#include "max_plus_1.c"
diff --git a/gcc/testsuite/gcc.target/aarch64/max_plus_5.c b/gcc/testsuite/gcc.target/aarch64/max_plus_5.c
new file mode 100644
index 00000000000..63f3b34424e
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/max_plus_5.c
@@ -0,0 +1,35 @@
+/* { dg-do run } */
+/* { dg-options "-O2 --save-temps" } */
+/* { dg-final { check-function-bodies "**" "" "" } } */
+
+/*
+** f1:
+**	adds	(w[0-9]+), w0, #4095
+**	csel	w0, \1, wzr, (cs|hi)
+**	ret
+*/
+/*
+** f2:
+**	adds	(w[0-9]+), w0, #4095
+**	csel	w0, \1, wzr, (cs|hi)
+**	ret
+*/
+/*
+** f3:
+**	adds	(w[0-9]+), w0, #4096
+**	csinc	w0, \1, wzr, hi
+**	ret
+*/
+/*
+** f4:
+**	adds	(w[0-9]+), w0, #4094
+**	csinv	w0, \1, wzr, cs
+**	ret
+*/
+
+#define TYPE uint32_t
+#define TYPE_MIN 0
+#define TYPE_MAX UINT32_MAX
+#define VALUE (uint32_t)-4095
+
+#include "max_plus_1.c"
diff --git a/gcc/testsuite/gcc.target/aarch64/max_plus_6.c b/gcc/testsuite/gcc.target/aarch64/max_plus_6.c
new file mode 100644
index 00000000000..ad592c69052
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/max_plus_6.c
@@ -0,0 +1,9 @@
+/* { dg-do run } */
+/* { dg-options "-O2 --save-temps" } */
+
+#define TYPE uint64_t
+#define TYPE_MIN 0
+#define TYPE_MAX UINT64_MAX
+#define VALUE (uint64_t)-2
+
+#include "max_plus_1.c"
diff --git a/gcc/testsuite/gcc.target/aarch64/max_plus_7.c b/gcc/testsuite/gcc.target/aarch64/max_plus_7.c
new file mode 100644
index 00000000000..ac9f27dec61
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/max_plus_7.c
@@ -0,0 +1,35 @@
+/* { dg-do run } */
+/* { dg-options "-O2 --save-temps" } */
+/* { dg-final { check-function-bodies "**" "" "" } } */
+
+/*
+** f1:
+**	adds	(x[0-9]+), x0, #3
+**	csel	x0, \1, xzr, (cs|hi)
+**	ret
+*/
+/*
+** f2:
+**	adds	(x[0-9]+), x0, #3
+**	csel	x0, \1, xzr, (cs|hi)
+**	ret
+*/
+/*
+** f3:
+**	adds	(x[0-9]+), x0, #4
+**	csinc	x0, \1, xzr, hi
+**	ret
+*/
+/*
+** f4:
+**	adds	(x[0-9]+), x0, #2
+**	csinv	x0, \1, xzr, cs
+**	ret
+*/
+
+#define TYPE uint64_t
+#define TYPE_MIN 0
+#define TYPE_MAX UINT64_MAX
+#define VALUE (uint64_t)-3
+
+#include "max_plus_1.c"
diff --git a/gcc/testsuite/gcc.target/aarch64/min_plus_1.c b/gcc/testsuite/gcc.target/aarch64/min_plus_1.c
new file mode 100644
index 00000000000..f4c9106dfca
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/min_plus_1.c
@@ -0,0 +1,149 @@
+/* { dg-do run } */
+/* { dg-options "-O2 --save-temps" } */
+/* { dg-final { check-function-bodies "**" "" "" } } */
+
+/*
+** f1:
+**	subs	(w[0-9]+), w0, #?4
+**	csel	w0, \1, wzr, l[te]
+**	ret
+*/
+/*
+** f2:
+**	subs	(w[0-9]+), w0, #?4
+**	csel	w0, \1, wzr, l[te]
+**	ret
+*/
+/*
+** f3:
+**	subs	(w[0-9]+), w0, #?3
+**	csinc	w0, \1, wzr, le
+**	ret
+*/
+/*
+** f4:
+**	subs	(w[0-9]+), w0, #?5
+**	csinv	w0, \1, wzr, lt
+**	ret
+*/
+
+#ifndef TYPE
+#define TYPE int32_t
+#define TYPE_MIN INT32_MIN
+#define TYPE_MAX INT32_MAX
+#define VALUE 4
+#endif
+
+#include <stdint.h>
+
+TYPE __attribute__((noipa))
+f1 (TYPE x)
+{
+  return (x < VALUE ? x - VALUE : 0);
+}
+
+TYPE __attribute__((noipa))
+f2 (TYPE x)
+{
+  return (x < VALUE ? x : VALUE) - VALUE;
+}
+
+TYPE __attribute__((noipa))
+f3 (TYPE x)
+{
+  return (x < VALUE ? x : VALUE) - (VALUE - 1);
+}
+
+TYPE __attribute__((noipa))
+f4 (TYPE x)
+{
+  return (x < VALUE ? x : VALUE) - (VALUE + 1);
+}
+
+TYPE __attribute__((noipa))
+f5 (TYPE x)
+{
+  return (x < VALUE ? x : VALUE) - (VALUE + 2);
+}
+
+TYPE __attribute__((noipa))
+f6 (TYPE x)
+{
+  return (x < VALUE ? x : VALUE) - (VALUE - 2);
+}
+
+int
+main (void)
+{
+  TYPE min_test = TYPE_MIN;
+  if (TYPE_MIN < 0 && VALUE > 0)
+    min_test += VALUE;
+
+  if (f1 (min_test) != min_test - VALUE)
+    __builtin_abort ();
+  if (f1 (VALUE - 1) != -1)
+    __builtin_abort ();
+  if (f1 (VALUE) != 0)
+    __builtin_abort ();
+  if (f1 (VALUE + 1) != 0)
+    __builtin_abort ();
+  if (f1 (TYPE_MAX) != 0)
+    __builtin_abort ();
+
+  if (f2 (min_test) != min_test - VALUE)
+    __builtin_abort ();
+  if (f2 (VALUE - 1) != -1)
+    __builtin_abort ();
+  if (f2 (VALUE) != 0)
+    __builtin_abort ();
+  if (f2 (VALUE + 1) != 0)
+    __builtin_abort ();
+  if (f2 (TYPE_MAX) != 0)
+    __builtin_abort ();
+
+  if (f3 (min_test) != min_test - VALUE + 1)
+    __builtin_abort ();
+  if (f3 (VALUE - 1) != 0)
+    __builtin_abort ();
+  if (f3 (VALUE) != 1)
+    __builtin_abort ();
+  if (f3 (VALUE + 1) != 1)
+    __builtin_abort ();
+  if (f3 (TYPE_MAX) != 1)
+    __builtin_abort ();
+
+  if (f4 (min_test + 1) != min_test - VALUE)
+    __builtin_abort ();
+  if (f4 (VALUE - 1) != -2)
+    __builtin_abort ();
+  if (f4 (VALUE) != -1)
+    __builtin_abort ();
+  if (f4 (VALUE + 1) != -1)
+    __builtin_abort ();
+  if (f4 (TYPE_MAX) != -1)
+    __builtin_abort ();
+
+  if (VALUE >= min_test + 2 && f5 (min_test + 2) != min_test - VALUE)
+    __builtin_abort ();
+  if (f5 (VALUE - 1) != -3)
+    __builtin_abort ();
+  if (f5 (VALUE) != -2)
+    __builtin_abort ();
+  if (f5 (VALUE + 1) != -2)
+    __builtin_abort ();
+  if (f5 (TYPE_MAX) != -2)
+    __builtin_abort ();
+
+  if (f6 (min_test) != min_test - VALUE + 2)
+    __builtin_abort ();
+  if (f6 (VALUE - 1) != 1)
+    __builtin_abort ();
+  if (f6 (VALUE) != 2)
+    __builtin_abort ();
+  if (f6 (VALUE + 1) != 2)
+    __builtin_abort ();
+  if (f6 (TYPE_MAX) != 2)
+    __builtin_abort ();
+
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.target/aarch64/min_plus_2.c b/gcc/testsuite/gcc.target/aarch64/min_plus_2.c
new file mode 100644
index 00000000000..bc0141b7264
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/min_plus_2.c
@@ -0,0 +1,35 @@
+/* { dg-do run } */
+/* { dg-options "-O2 --save-temps" } */
+/* { dg-final { check-function-bodies "**" "" "" } } */
+
+/*
+** f1:
+**	subs	(x[0-9]+), x0, #?4094
+**	csel	x0, \1, xzr, l[te]
+**	ret
+*/
+/*
+** f2:
+**	subs	(x[0-9]+), x0, #?4094
+**	csel	x0, \1, xzr, l[te]
+**	ret
+*/
+/*
+** f3:
+**	subs	(x[0-9]+), x0, #?4093
+**	csinc	x0, \1, xzr, le
+**	ret
+*/
+/*
+** f4:
+**	subs	(x[0-9]+), x0, #?4095
+**	csinv	x0, \1, xzr, lt
+**	ret
+*/
+
+#define TYPE int64_t
+#define TYPE_MIN INT64_MIN
+#define TYPE_MAX INT64_MAX
+#define VALUE 4094
+
+#include "min_plus_1.c"
diff --git a/gcc/testsuite/gcc.target/aarch64/min_plus_3.c b/gcc/testsuite/gcc.target/aarch64/min_plus_3.c
new file mode 100644
index 00000000000..1808e4b0cbf
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/min_plus_3.c
@@ -0,0 +1,35 @@
+/* { dg-do run } */
+/* { dg-options "-O2 --save-temps" } */
+/* { dg-final { check-function-bodies "**" "" "" } } */
+
+/*
+** f1:
+**	subs	(w[0-9]+), w0, #?4095
+**	csel	w0, \1, wzr, l[te]
+**	ret
+*/
+/*
+** f2:
+**	subs	(w[0-9]+), w0, #?4095
+**	csel	w0, \1, wzr, l[te]
+**	ret
+*/
+/*
+** f3:
+**	subs	(w[0-9]+), w0, #?4094
+**	csinc	w0, \1, wzr, le
+**	ret
+*/
+/*
+** f4:
+**	subs	(w[0-9]+), w0, #?4096
+**	csinv	w0, \1, wzr, lt
+**	ret
+*/
+
+#define TYPE int32_t
+#define TYPE_MIN INT32_MIN
+#define TYPE_MAX INT32_MAX
+#define VALUE 4095
+
+#include "min_plus_1.c"
diff --git a/gcc/testsuite/gcc.target/aarch64/min_plus_4.c b/gcc/testsuite/gcc.target/aarch64/min_plus_4.c
new file mode 100644
index 00000000000..6c581fed61d
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/min_plus_4.c
@@ -0,0 +1,30 @@
+/* { dg-do run } */
+/* { dg-options "-O2 --save-temps" } */
+/* { dg-final { check-function-bodies "**" "" "" } } */
+
+/*
+** f1:
+**	subs	(x[0-9]+), x0, #?4096
+**	csel	x0, \1, xzr, l[te]
+**	ret
+*/
+/*
+** f2:
+**	subs	(x[0-9]+), x0, #?4096
+**	csel	x0, \1, xzr, l[te]
+**	ret
+*/
+/*
+** f3:
+**	subs	(x[0-9]+), x0, #?4095
+**	csinc	x0, \1, xzr, le
+**	ret
+*/
+/* f4 out of range */
+
+#define TYPE int64_t
+#define TYPE_MIN INT64_MIN
+#define TYPE_MAX INT64_MAX
+#define VALUE 4096
+
+#include "min_plus_1.c"
diff --git a/gcc/testsuite/gcc.target/aarch64/min_plus_5.c b/gcc/testsuite/gcc.target/aarch64/min_plus_5.c
new file mode 100644
index 00000000000..97542d50765
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/min_plus_5.c
@@ -0,0 +1,35 @@
+/* { dg-do run } */
+/* { dg-options "-O2 --save-temps" } */
+/* { dg-final { check-function-bodies "**" "" "" } } */
+
+/*
+** f1:
+**	subs	(w[0-9]+), w0, #?4095
+**	csel	w0, \1, wzr, (cc|ls)
+**	ret
+*/
+/*
+** f2:
+**	subs	(w[0-9]+), w0, #?4095
+**	csel	w0, \1, wzr, (cc|ls)
+**	ret
+*/
+/*
+** f3:
+**	subs	(w[0-9]+), w0, #?4094
+**	csinc	w0, \1, wzr, ls
+**	ret
+*/
+/*
+** f4:
+**	subs	(w[0-9]+), w0, #?4096
+**	csinv	w0, \1, wzr, cc
+**	ret
+*/
+
+#define TYPE uint32_t
+#define TYPE_MIN 0
+#define TYPE_MAX UINT32_MAX
+#define VALUE 4095
+
+#include "min_plus_1.c"
diff --git a/gcc/testsuite/gcc.target/aarch64/min_plus_6.c b/gcc/testsuite/gcc.target/aarch64/min_plus_6.c
new file mode 100644
index 00000000000..176533cb2e1
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/min_plus_6.c
@@ -0,0 +1,9 @@
+/* { dg-do run } */
+/* { dg-options "-O2 --save-temps" } */
+
+#define TYPE uint64_t
+#define TYPE_MIN 0
+#define TYPE_MAX UINT64_MAX
+#define VALUE 1
+
+#include "min_plus_1.c"
diff --git a/gcc/testsuite/gcc.target/aarch64/min_plus_7.c b/gcc/testsuite/gcc.target/aarch64/min_plus_7.c
new file mode 100644
index 00000000000..d6a217a5168
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/min_plus_7.c
@@ -0,0 +1,35 @@
+/* { dg-do run } */
+/* { dg-options "-O2 --save-temps" } */
+/* { dg-final { check-function-bodies "**" "" "" } } */
+
+/*
+** f1:
+**	subs	(x[0-9]+), x0, #?2
+**	csel	x0, \1, xzr, (cc|ls)
+**	ret
+*/
+/*
+** f2:
+**	subs	(x[0-9]+), x0, #?2
+**	csel	x0, \1, xzr, (cc|ls)
+**	ret
+*/
+/*
+** f3:
+**	subs	(x[0-9]+), x0, #?1
+**	csinc	x0, \1, xzr, ls
+**	ret
+*/
+/*
+** f4:
+**	subs	(x[0-9]+), x0, #?3
+**	csinv	x0, \1, xzr, cc
+**	ret
+*/
+
+#define TYPE uint64_t
+#define TYPE_MIN 0
+#define TYPE_MAX UINT64_MAX
+#define VALUE 2
+
+#include "min_plus_1.c"


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-02-15 18:10 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-15 18:10 [gcc r12-7249] aarch64: Fix subs_compare_2.c regression [PR100874] Richard Sandiford

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).