This patch is a ping of a nvptx backend patch from July 2024:
https://gcc.gnu.org/pipermail/gcc-patches/2024-July/658462.html

Using the copysign RTX code instead of an UNSPEC allows the RTL
optimizers to perform simplifications and compile-time evaluation
of constants, where an UNSPEC is a "black-box" that has no
semantics (outside the backend).

The good news is that evrp handling of isfinite has been fixed, so
this patch now has no regressions on nvptx-none hosted on x86_64,
using make and make -k check.  Ok for mainline?


2026-11-09  Roger Sayle  <[email protected]>

gcc/ChangeLog
        * config/nvptx/nptx.md (UNSPEC_COPYSIGN): No longer required.
        (UNSPEC_ISFINITE): New UNSPEC.
        (UNSPEC_ISNORMAL): Likewise.
        (*cmp<mode>): Rename to...
        (cmp<mode>): Remove '*' prefix to generate gen_cmp{s,d}f.
        (copysign<mode>3): Replace UNSPEC_COPYSIGN with copysign RTX.
        (*setcc_isfinite<mode>): New define_insn using UNSPEC_ISFINITE.
        (isfinite<mode>2): Expand isfinite.
        (*setcc_isnormal<mode>): New define_insn using UNSPEC_ISNORMAL.
        (isnormal<mode>2): Expand isnormal.

gcc/testsuite/ChangeLog
        * gcc.target/nvptx/isfinite.c: New test case.
        * gcc.target/nvptx/isnormal.c: Likewise.


Thanks in advance,
Roger
--

diff --git a/gcc/config/nvptx/nvptx.md b/gcc/config/nvptx/nvptx.md
index 21731da..7441912 100644
--- a/gcc/config/nvptx/nvptx.md
+++ b/gcc/config/nvptx/nvptx.md
@@ -21,13 +21,14 @@
 (define_c_enum "unspec" [
    UNSPEC_ARG_REG
 
-   UNSPEC_COPYSIGN
    UNSPEC_LOG2
    UNSPEC_EXP2
    UNSPEC_SIN
    UNSPEC_COS
    UNSPEC_TANH
    UNSPEC_ISINF
+   UNSPEC_ISFINITE
+   UNSPEC_ISNORMAL
 
    UNSPEC_FPINT_FLOOR
    UNSPEC_FPINT_BTRUNC
@@ -889,7 +890,7 @@
   ""
   "%.\\tsetp%c1\\t%0, %2, %3;")
 
-(define_insn "*cmp<mode>"
+(define_insn "cmp<mode>"
   [(set (match_operand:BI 0 "nvptx_register_operand" "=R")
        (match_operator:BI 1 "nvptx_float_comparison_operator"
           [(match_operand:SDFM 2 "nvptx_register_operand" "R")
@@ -1254,9 +1255,8 @@
 
 (define_insn "copysign<mode>3"
   [(set (match_operand:SDFM 0 "nvptx_register_operand" "=R")
-       (unspec:SDFM [(match_operand:SDFM 1 "nvptx_nonmemory_operand" "RF")
-                     (match_operand:SDFM 2 "nvptx_nonmemory_operand" "RF")]
-                     UNSPEC_COPYSIGN))]
+       (copysign:SDFM (match_operand:SDFM 1 "nvptx_nonmemory_operand" "RF")
+                      (match_operand:SDFM 2 "nvptx_nonmemory_operand" "RF")))]
   ""
   "%.\\tcopysign%t0\\t%0, %2, %1;")
 
@@ -1331,6 +1331,8 @@
   "flag_unsafe_math_optimizations"
   "%.\\tex2.approx%t0\\t%0, %1;")
 
+;; FP classify predicates
+
 (define_insn "setcc_isinf<mode>"
   [(set (match_operand:BI 0 "nvptx_register_operand" "=R")
        (unspec:BI [(match_operand:SDFM 1 "nvptx_register_operand" "R")]
@@ -1350,6 +1352,50 @@
   DONE;
 })
 
+(define_insn "setcc_isfinite<mode>"
+  [(set (match_operand:BI 0 "nvptx_register_operand" "=R")
+       (unspec:BI [(match_operand:SDFM 1 "nvptx_register_operand" "R")]
+                  UNSPEC_ISFINITE))]
+  ""
+  "%.\\ttestp.finite%t1\\t%0, %1;")
+
+(define_expand "isfinite<mode>2"
+  [(set (match_operand:SI 0 "nvptx_register_operand" "=R")
+       (unspec:SI [(match_operand:SDFM 1 "nvptx_register_operand" "R")]
+                  UNSPEC_ISFINITE))]
+  ""
+{
+  rtx pred = gen_reg_rtx (BImode);
+  emit_insn (gen_setcc_isfinite<mode> (pred, operands[1]));
+  emit_insn (gen_setccsi_from_bi (operands[0], pred));
+  DONE;
+})
+
+(define_insn "setcc_isnormal<mode>"
+  [(set (match_operand:BI 0 "nvptx_register_operand" "=R")
+       (unspec:BI [(match_operand:SDFM 1 "nvptx_register_operand" "R")]
+                  UNSPEC_ISNORMAL))]
+  ""
+  "%.\\ttestp.normal%t1\\t%0, %1;")
+
+(define_expand "isnormal<mode>2"
+  [(set (match_operand:SI 0 "nvptx_register_operand" "=R")
+       (unspec:SI [(match_operand:SDFM 1 "nvptx_register_operand" "R")]
+                  UNSPEC_ISNORMAL))]
+  ""
+{
+  rtx pred1 = gen_reg_rtx (BImode);
+  rtx pred2 = gen_reg_rtx (BImode);
+  rtx pred3 = gen_reg_rtx (BImode);
+  rtx zero = CONST0_RTX (<MODE>mode);
+  rtx cmp = gen_rtx_fmt_ee (NE, BImode, operands[1], zero);
+  emit_insn (gen_cmp<mode> (pred1, cmp, operands[1], zero));
+  emit_insn (gen_setcc_isnormal<mode> (pred2, operands[1]));
+  emit_insn (gen_andbi3 (pred3, pred1, pred2));
+  emit_insn (gen_setccsi_from_bi (operands[0], pred3));
+  DONE;
+})
+
 ;; HFmode floating point arithmetic.
 
 (define_insn "addhf3"
diff --git a/gcc/testsuite/gcc.target/nvptx/isfinite.c 
b/gcc/testsuite/gcc.target/nvptx/isfinite.c
new file mode 100644
index 0000000..83099fc
--- /dev/null
+++ b/gcc/testsuite/gcc.target/nvptx/isfinite.c
@@ -0,0 +1,9 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+int foo(double x)
+{
+  return __builtin_isfinite(x);
+}
+
+/* { dg-final { scan-assembler-times "testp.finite.f64" 1 } } */
diff --git a/gcc/testsuite/gcc.target/nvptx/isnormal.c 
b/gcc/testsuite/gcc.target/nvptx/isnormal.c
new file mode 100644
index 0000000..83c4fb6
--- /dev/null
+++ b/gcc/testsuite/gcc.target/nvptx/isnormal.c
@@ -0,0 +1,9 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+int isnormal(double x)
+{
+  return __builtin_isnormal(x);
+}
+
+/* { dg-final { scan-assembler-times "testp.normal.f64" 1 } } */

Reply via email to