This is a follow-up to
https://gcc.gnu.org/pipermail/gcc-patches/2026-October/736381.html

If there are no further comments I will push this the coming week.

-- >8 --

From: Stefan Schulze Frielinghaus <[email protected]>

Starting with r0-104773-g0d697034b7eecd which landed in GCC 4.6 (which
introduced initial C++11 and std::nullptr_t support), arguments of type
nullptr_t / std::nullptr_t / decltype(nullptr) are passed in registers.
However, return values are erroneously returned in memory violating the
psABI.

Fixed by handling NULLPTR_TYPE in s390_return_in_memory, similarly to
how it is already handled in s390_function_arg_integer, so that it is
returned in a register.

A note is emitted at most once per function.

gcc/ChangeLog:

        * config/s390/s390.cc (s390_return_in_memory): Handle
        NULLPTR_TYPE.

gcc/testsuite/ChangeLog:

        * g++.target/s390/nullptr-1.C: New test.
        * g++.target/s390/nullptr-2.C: New test.
        * gcc.target/s390/nullptr-1.c: New test.
        * gcc.target/s390/nullptr-2.c: New test.
        * gcc.target/s390/nullptr-3.c: New test.
---
 gcc/config/s390/s390.cc                   |  19 ++++
 gcc/testsuite/g++.target/s390/nullptr-1.C |  31 ++++++
 gcc/testsuite/g++.target/s390/nullptr-2.C |   7 ++
 gcc/testsuite/gcc.target/s390/nullptr-1.c | 110 ++++++++++++++++++++++
 gcc/testsuite/gcc.target/s390/nullptr-2.c |  31 ++++++
 gcc/testsuite/gcc.target/s390/nullptr-3.c |   7 ++
 6 files changed, 205 insertions(+)
 create mode 100644 gcc/testsuite/g++.target/s390/nullptr-1.C
 create mode 100644 gcc/testsuite/g++.target/s390/nullptr-2.C
 create mode 100644 gcc/testsuite/gcc.target/s390/nullptr-1.c
 create mode 100644 gcc/testsuite/gcc.target/s390/nullptr-2.c
 create mode 100644 gcc/testsuite/gcc.target/s390/nullptr-3.c

diff --git a/gcc/config/s390/s390.cc b/gcc/config/s390/s390.cc
index 895b1e79298..be4f67cb851 100644
--- a/gcc/config/s390/s390.cc
+++ b/gcc/config/s390/s390.cc
@@ -13431,6 +13431,25 @@ s390_return_in_memory (const_tree type, const_tree 
fundecl ATTRIBUTE_UNUSED)
       || SCALAR_FLOAT_TYPE_P (type))
     return int_size_in_bytes (type) > 8;
 
+  if (TREE_CODE (type) == NULLPTR_TYPE)
+    {
+      /* Emit the note at most once per function.  */
+      static unsigned last_reported_fndecl_uid_nullptr;
+      if (warn_psabi && current_function_decl
+         && DECL_UID (current_function_decl)
+              != last_reported_fndecl_uid_nullptr)
+       {
+         last_reported_fndecl_uid_nullptr = DECL_UID (current_function_decl);
+         const char *url
+           = CHANGES_ROOT_URL "gcc-17/changes.html#s390_return_nullptr_type";
+         inform (input_location,
+                 "the ABI for returning %qT changed %{in GCC 17.1%}; it is "
+                 "now returned in a register instead of in memory",
+                 type, url);
+       }
+      return false;
+    }
+
   /* vector types which fit into a VR.  */
   if (TARGET_VX_ABI
       && VECTOR_TYPE_P (type)
diff --git a/gcc/testsuite/g++.target/s390/nullptr-1.C 
b/gcc/testsuite/g++.target/s390/nullptr-1.C
new file mode 100644
index 00000000000..3bfd2d7d86c
--- /dev/null
+++ b/gcc/testsuite/g++.target/s390/nullptr-1.C
@@ -0,0 +1,31 @@
+// { dg-do compile { target c++11 } }
+// { dg-options "-O2 -Wpsabi" }
+
+// Check that -Wpsabi notes the changed ABI for returning std::nullptr_t and
+// that the note is emitted only once per function.  Notes are emitted for call
+// sides and function definitions.
+
+typedef decltype (nullptr) nullptr_type;
+
+extern nullptr_type foo ();
+extern nullptr_type bar ();
+
+void
+test_call_1 ()
+{
+  foo ();   // { dg-message "the ABI for returning 'nullptr_type' .aka 
'std::nullptr_t'. changed in GCC 17.1" }
+  foo ();
+  bar ();
+}
+
+void
+test_call_2 ()
+{
+  bar ();   // { dg-message "the ABI for returning 'nullptr_type' .aka 
'std::nullptr_t'. changed in GCC 17.1" }
+}
+
+nullptr_type
+test_def () // { dg-message "the ABI for returning 'nullptr_type' .aka 
'std::nullptr_t'. changed in GCC 17.1" }
+{
+  return nullptr;
+}
diff --git a/gcc/testsuite/g++.target/s390/nullptr-2.C 
b/gcc/testsuite/g++.target/s390/nullptr-2.C
new file mode 100644
index 00000000000..d622a563bd0
--- /dev/null
+++ b/gcc/testsuite/g++.target/s390/nullptr-2.C
@@ -0,0 +1,7 @@
+// { dg-do compile { target c++11 } }
+// { dg-options "-O2 -Wno-psabi" }
+
+// Check that -Wno-psabi suppresses the warning/note about the changed ABI for
+// returning std::nullptr_t.
+
+#include "nullptr-1.C"
diff --git a/gcc/testsuite/gcc.target/s390/nullptr-1.c 
b/gcc/testsuite/gcc.target/s390/nullptr-1.c
new file mode 100644
index 00000000000..07336a0f852
--- /dev/null
+++ b/gcc/testsuite/gcc.target/s390/nullptr-1.c
@@ -0,0 +1,110 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -std=c23 -Wno-psabi" } */
+/* { dg-final { check-function-bodies "**" "" "" } } */
+
+/* ABI tests for passing/returning values of type nullptr_t.  */
+
+#include <stddef.h>
+
+/*
+** test_ret_callee:
+**     lghi    %r2,0
+**     br      %r14
+*/
+[[gnu::noipa]]
+nullptr_t
+test_ret_callee (void)
+{
+  return nullptr;
+}
+
+/*
+** test_ret_caller:
+**     ...
+**     brasl   %r14,test_ret_callee@PLT
+**     stg     %r2,160\(%r15\)
+**     ...
+*/
+void
+test_ret_caller (void)
+{
+  volatile nullptr_t x = test_ret_callee ();
+}
+
+/*
+** test_arg_1_call:
+**     ...
+**     lghi    %r2,0
+**     ...
+*/
+extern void test_arg_1 (nullptr_t);
+void
+test_arg_1_call (void)
+{
+  test_arg_1 (nullptr);
+}
+
+/*
+** test_arg_2_call:
+**     ...
+**     lghi    %r3,0
+**     ...
+*/
+extern void test_arg_2 (long, nullptr_t);
+void
+test_arg_2_call (void)
+{
+  test_arg_2 (42, nullptr);
+}
+
+/*
+** test_arg_3_call:
+**     ...
+**     lghi    %r4,0
+**     ...
+*/
+extern void test_arg_3 (long, long, nullptr_t);
+void
+test_arg_3_call (void)
+{
+  test_arg_3 (42, 42, nullptr);
+}
+
+/*
+** test_arg_4_call:
+**     ...
+**     lghi    %r5,0
+**     ...
+*/
+extern void test_arg_4 (long, long, long, nullptr_t);
+void
+test_arg_4_call (void)
+{
+  test_arg_4 (42, 42, 42, nullptr);
+}
+
+/*
+** test_arg_5_call:
+**     ...
+**     lghi    %r6,0
+**     ...
+*/
+extern void test_arg_5 (long, long, long, long, nullptr_t);
+void
+test_arg_5_call (void)
+{
+  test_arg_5 (42, 42, 42, 42, nullptr);
+}
+
+/*
+** test_arg_6_call:
+**     ...
+**     (mvghi  160\(%r15\),0|stg       %r[0-9]+,160\(%r15\))
+**     ...
+*/
+extern void test_arg_6 (long, long, long, long, long, nullptr_t);
+void
+test_arg_6_call (void)
+{
+  test_arg_6 (42, 42, 42, 42, 42, nullptr);
+}
diff --git a/gcc/testsuite/gcc.target/s390/nullptr-2.c 
b/gcc/testsuite/gcc.target/s390/nullptr-2.c
new file mode 100644
index 00000000000..f426322692c
--- /dev/null
+++ b/gcc/testsuite/gcc.target/s390/nullptr-2.c
@@ -0,0 +1,31 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -std=c23 -Wpsabi" } */
+
+/* Check that -Wpsabi notes the changed ABI for returning nullptr_t and
+   that the note is emitted only once per function.  Notes are emitted at call
+   sides or for function definitions.  */
+
+#include <stddef.h>
+
+extern nullptr_t foo (void);
+extern nullptr_t bar (void);
+
+void
+test_call_1 (void)
+{
+  foo (); /* { dg-message "the ABI for returning 'nullptr_t' changed in GCC 
17.1" } */
+  foo ();
+  bar ();
+}
+
+void
+test_call_2 (void)
+{
+  bar (); /* { dg-message "the ABI for returning 'nullptr_t' changed in GCC 
17.1" } */
+}
+
+nullptr_t
+test_def (void)
+{         /* { dg-message "the ABI for returning 'nullptr_t' changed in GCC 
17.1" } */
+  return nullptr;
+}
diff --git a/gcc/testsuite/gcc.target/s390/nullptr-3.c 
b/gcc/testsuite/gcc.target/s390/nullptr-3.c
new file mode 100644
index 00000000000..3466f64cc31
--- /dev/null
+++ b/gcc/testsuite/gcc.target/s390/nullptr-3.c
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -std=c23 -Wno-psabi" } */
+
+/* Check that -Wno-psabi suppresses the warning about the changed ABI for
+   returning nullptr_t.  */
+
+#include "nullptr-2.c"
-- 
2.55.0

Reply via email to