[gcc r14-8495] x86: Save callee-saved registers in noreturn functions for -O0/-Og

H.J. Lu hjl@gcc.gnu.org
Mon Jan 29 13:29:34 GMT 2024


https://gcc.gnu.org/g:291f75fa1bc6a23c6184bb99c726074b13f2f18e

commit r14-8495-g291f75fa1bc6a23c6184bb99c726074b13f2f18e
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Sat Jan 27 05:48:45 2024 -0800

    x86: Save callee-saved registers in noreturn functions for -O0/-Og
    
    Save callee-saved registers in noreturn functions for -O0/-Og so that
    debugger can restore callee-saved registers in caller's frame.
    
    Also add the TREE_THIS_VOLATILE check to minimize noreturn attribute
    lookup.
    
    gcc/
    
            PR target/38534
            * config/i386/i386-options.cc (ix86_set_func_type): Save
            callee-saved registers in noreturn functions for -O0/-Og.
    
    gcc/testsuite/
    
            PR target/38534
            * gcc.target/i386/pr38534-5.c: New file.
            * gcc.target/i386/pr38534-6.c: Likewise.

Diff:
---
 gcc/config/i386/i386-options.cc           | 12 ++++++++----
 gcc/testsuite/gcc.target/i386/pr38534-5.c | 26 ++++++++++++++++++++++++++
 gcc/testsuite/gcc.target/i386/pr38534-6.c | 26 ++++++++++++++++++++++++++
 3 files changed, 60 insertions(+), 4 deletions(-)

diff --git a/gcc/config/i386/i386-options.cc b/gcc/config/i386/i386-options.cc
index 473f5359fc93..8f5ce817630c 100644
--- a/gcc/config/i386/i386-options.cc
+++ b/gcc/config/i386/i386-options.cc
@@ -3381,9 +3381,10 @@ static void
 ix86_set_func_type (tree fndecl)
 {
   /* No need to save and restore callee-saved registers for a noreturn
-     function with nothrow or compiled with -fno-exceptions.
+     function with nothrow or compiled with -fno-exceptions unless when
+     compiling with -O0 or -Og.
 
-     NB: Don't use TREE_THIS_VOLATILE to check if this is a noreturn
+     NB: Can't use just TREE_THIS_VOLATILE to check if this is a noreturn
      function.  The local-pure-const pass turns an interrupt function
      into a noreturn function by setting TREE_THIS_VOLATILE.  Normally
      the local-pure-const pass is run after ix86_set_func_type is called.
@@ -3391,8 +3392,11 @@ ix86_set_func_type (tree fndecl)
      function is marked as noreturn in the IR output, which leads the
      incompatible attribute error in LTO1.  */
   bool has_no_callee_saved_registers
-    = (((TREE_NOTHROW (fndecl) || !flag_exceptions)
-	&& lookup_attribute ("noreturn", DECL_ATTRIBUTES (fndecl)))
+    = ((TREE_THIS_VOLATILE (fndecl)
+	&& lookup_attribute ("noreturn", DECL_ATTRIBUTES (fndecl))
+	&& optimize
+	&& !optimize_debug
+	&& (TREE_NOTHROW (fndecl) || !flag_exceptions))
        || lookup_attribute ("no_callee_saved_registers",
 			    TYPE_ATTRIBUTES (TREE_TYPE (fndecl))));
 
diff --git a/gcc/testsuite/gcc.target/i386/pr38534-5.c b/gcc/testsuite/gcc.target/i386/pr38534-5.c
new file mode 100644
index 000000000000..91c0c0f8c593
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr38534-5.c
@@ -0,0 +1,26 @@
+/* { dg-do compile } */
+/* { dg-options "-O0 -mtune-ctrl=^prologue_using_move,^epilogue_using_move" } */
+
+#define ARRAY_SIZE 256
+
+extern int array[ARRAY_SIZE][ARRAY_SIZE][ARRAY_SIZE];
+extern int value (int, int, int)
+#ifndef __x86_64__
+__attribute__ ((regparm(3)))
+#endif
+;
+
+void
+__attribute__((noreturn))
+no_return_to_caller (void)
+{
+  unsigned i, j, k;
+  for (i = ARRAY_SIZE; i > 0; --i)
+    for (j = ARRAY_SIZE; j > 0; --j)
+      for (k = ARRAY_SIZE; k > 0; --k)
+	array[i - 1][j - 1][k - 1] = value (i, j, k);
+  while (1);
+}
+
+/* { dg-final { scan-assembler "push" } } */
+/* { dg-final { scan-assembler-not "pop" } } */
diff --git a/gcc/testsuite/gcc.target/i386/pr38534-6.c b/gcc/testsuite/gcc.target/i386/pr38534-6.c
new file mode 100644
index 000000000000..cf1463a9c666
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr38534-6.c
@@ -0,0 +1,26 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -mtune-ctrl=^prologue_using_move,^epilogue_using_move" } */
+
+#define ARRAY_SIZE 256
+
+extern int array[ARRAY_SIZE][ARRAY_SIZE][ARRAY_SIZE];
+extern int value (int, int, int)
+#ifndef __x86_64__
+__attribute__ ((regparm(3)))
+#endif
+;
+
+void
+__attribute__((noreturn, optimize("-Og")))
+no_return_to_caller (void)
+{
+  unsigned i, j, k;
+  for (i = ARRAY_SIZE; i > 0; --i)
+    for (j = ARRAY_SIZE; j > 0; --j)
+      for (k = ARRAY_SIZE; k > 0; --k)
+	array[i - 1][j - 1][k - 1] = value (i, j, k);
+  while (1);
+}
+
+/* { dg-final { scan-assembler "push" } } */
+/* { dg-final { scan-assembler-not "pop" } } */


More information about the Gcc-cvs mailing list