diff mbox series

[PATCH-2] Builtins: Fold isfinite on IBM long double to isfinite on high-order double [PR97786]

Message ID 3ad56ccc-0787-4035-b85e-002390748f64@linux.ibm.com
State New
Headers show
Series [PATCH-2] Builtins: Fold isfinite on IBM long double to isfinite on high-order double [PR97786] | expand

Commit Message

HAO CHEN GUI Aug. 16, 2024, 9:47 a.m. UTC
Hi,
  This patch folds builtin_isfinite on IBM long double to builtin_isfinite
on its high-order double.

  The isfinite_optab was already implemented in this patch.
https://gcc.gnu.org/g:44eb45c2ef7192eb6a811fd46fcb2c7fbeb6f865

  Bootstrapped and tested on powerpc64-linux BE and LE with no
regressions. Is it OK for the trunk?

Thanks
Gui Haochen

ChangeLog
Builtins: Fold isfinite on IBM long double to isfinite on high-order double

For IBM long double, INF and NAN is encoded in the high-order double value
only.  So the builtin_isfinite on IBM long double can be folded to
builtin_isfinite on double type.  As former patch implemented DFmode
isfinite_optab, this patch converts builtin_isfinite on IBM long double to
builtin_isfinite on double type if the DFmode isfinite_optab exists.

gcc/
	PR target/97786
	* builtins.cc (fold_builtin_interclass_mathfn): Fold isfinite on
	IBM long double to isfinite on high-order double when DFmode
	isfinite_optab exists.

gcc/testsuite/
	PR target/97786
	* gcc.target/powerpc/pr97786-6.c: New test.

patch.diff
diff mbox series

Patch

diff --git a/gcc/builtins.cc b/gcc/builtins.cc
index c20bd7b5f31..c3d8ce5313a 100644
--- a/gcc/builtins.cc
+++ b/gcc/builtins.cc
@@ -9601,6 +9601,15 @@  fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
 	    type = double_type_node;
 	    mode = DFmode;
 	    arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
+
+	    /* If isfinite icode exists, build the call with high-order
+	       double value only.  */
+	    tree const isfinite_fn = builtin_decl_explicit (BUILT_IN_ISFINITE);
+	    if (interclass_mathfn_icode (arg, isfinite_fn) != CODE_FOR_nothing)
+	      {
+		result = build_call_expr (isfinite_fn, 1, arg);
+		return result;
+	      }
 	  }
 	get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
 	real_from_string (&r, buf);
diff --git a/gcc/testsuite/gcc.target/powerpc/pr97786-6.c b/gcc/testsuite/gcc.target/powerpc/pr97786-6.c
new file mode 100644
index 00000000000..1b25bcecd5a
--- /dev/null
+++ b/gcc/testsuite/gcc.target/powerpc/pr97786-6.c
@@ -0,0 +1,12 @@ 
+/* { dg-do compile } */
+/* { dg-require-effective-target ppc_float128_hw } */
+/* { dg-options "-O2 -mdejagnu-cpu=power9 -mvsx -mabi=ibmlongdouble -Wno-psabi" } */
+/* { dg-require-effective-target powerpc_vsx } */
+
+int test1 (long double x)
+{
+  return __builtin_isfinite (x);
+}
+
+/* { dg-final { scan-assembler-not {\mfcmpu\M} } } */
+/* { dg-final { scan-assembler {\mxststdcdp\M} } } */