From fd4d44300e125bfe0b9ee8cdb37c75e2dc8d1190 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20Chigot?= <clement.chigot@atos.net>
Date: Thu, 26 Mar 2020 14:20:42 -0500
Subject: [PATCH] gcc/config/rs6000: fix long double builtins for AIX
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Descritpion:
 * This patch fixes the explicit call made by some C long double
   builtins when long doubles are on 64bit for AIX.

Tests:
 * AIX 7.2, 7.1, 6.1: Build/Tests: OK

Changelog:
2020-04-27 Clément Chigot <clement.chigot@atos.net
 * config/rs6000/rs6000-call.c (rs6000_init_builtins): Fix explicit
 for fmodl, frexpl, ldexpl and modfl builtins.
---
 gcc/config/rs6000/rs6000-call.c | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)

diff --git a/gcc/config/rs6000/rs6000-call.c b/gcc/config/rs6000/rs6000-call.c
index e08621ace27..42f8823cf9f 100644
--- a/gcc/config/rs6000/rs6000-call.c
+++ b/gcc/config/rs6000/rs6000-call.c
@@ -12032,10 +12032,31 @@ rs6000_init_builtins (void)
   def_builtin ("__builtin_cpu_is", ftype, RS6000_BUILTIN_CPU_IS);
   def_builtin ("__builtin_cpu_supports", ftype, RS6000_BUILTIN_CPU_SUPPORTS);
 
-  /* AIX libm provides clog as __clog.  */
-  if (TARGET_XCOFF &&
-      (tdecl = builtin_decl_explicit (BUILT_IN_CLOG)) != NULL_TREE)
-    set_user_assembler_name (tdecl, "__clog");
+  if (TARGET_XCOFF)
+    {
+      /* AIX libm provides clog as __clog.  */
+      if ((tdecl = builtin_decl_explicit (BUILT_IN_CLOG)) != NULL_TREE)
+        set_user_assembler_name (tdecl, "__clog");
+
+      /*
+        When long double is 64bit, some long double builtins of libc functions
+        (like  __builtin_frexpl) must not call the long double libc function (frexpl)
+        but the double version (frexp).
+        The reason is that these long double libc version are expected long double on
+        128 bits only.
+      */
+      if (! TARGET_LONG_DOUBLE_128)
+        {
+          if ((tdecl = builtin_decl_explicit (BUILT_IN_FMODL)) != NULL_TREE)
+            set_user_assembler_name (tdecl, "fmod");
+          if ((tdecl = builtin_decl_explicit (BUILT_IN_FREXPL)) != NULL_TREE)
+            set_user_assembler_name (tdecl, "frexp");
+          if ((tdecl = builtin_decl_explicit (BUILT_IN_LDEXPL)) != NULL_TREE)
+            set_user_assembler_name (tdecl, "ldexp");
+          if ((tdecl = builtin_decl_explicit (BUILT_IN_MODFL)) != NULL_TREE)
+            set_user_assembler_name (tdecl, "modf");
+        }
+   }
 
 #ifdef SUBTARGET_INIT_BUILTINS
   SUBTARGET_INIT_BUILTINS;
-- 
2.25.0

