re PR middle-end/37843 (unaligned stack in main due to tail call optimization)
authorH.J. Lu <hongjiu.lu@intel.com>
Tue, 25 Nov 2008 15:33:27 +0000 (15:33 +0000)
committerH.J. Lu <hjl@gcc.gnu.org>
Tue, 25 Nov 2008 15:33:27 +0000 (07:33 -0800)
gcc/

2008-11-25  H.J. Lu  <hongjiu.lu@intel.com>
    Joey Ye  <joey.ye@intel.com>

PR middle-end/37843
* config/i386/i386.c (ix86_function_ok_for_sibcall): Return
false if we need to align the outgoing stack.
(ix86_update_stack_boundary): Check parm_stack_boundary.

gcc/testsuite/

2008-11-25  H.J. Lu  <hongjiu.lu@intel.com>

PR middle-end/37843
* gcc.target/i386/align-main-3.c: New.
* gcc.target/i386/pr37843-1.c: Likewise.
* gcc.target/i386/pr37843-2.c: Likewise.
* gcc.target/i386/pr37843-3.c: Likewise.

Co-Authored-By: Joey Ye <joey.ye@intel.com>
From-SVN: r142193

gcc/ChangeLog
gcc/config/i386/i386.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.target/i386/align-main-3.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr37843-1.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr37843-2.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr37843-3.c [new file with mode: 0644]

index 148e7b2f3094e707190568189cf502859f8c4489..bf00b6838941a3784e45d18b2dac6457bc68eb3b 100644 (file)
@@ -1,3 +1,11 @@
+2008-11-25  H.J. Lu  <hongjiu.lu@intel.com>
+           Joey Ye  <joey.ye@intel.com>
+
+       PR middle-end/37843
+       * config/i386/i386.c (ix86_function_ok_for_sibcall): Return
+       false if we need to align the outgoing stack.
+       (ix86_update_stack_boundary): Check parm_stack_boundary.
+
 2008-11-25  Richard Guenther  <rguenther@suse.de>
 
        PR middle-end/38151
index 8f4680db4bc08d69830c03d70fc991ebfe026f02..1f7d506fa49de6fdd1f24b99258ec8a0b4774999 100644 (file)
@@ -4112,6 +4112,11 @@ ix86_function_ok_for_sibcall (tree decl, tree exp)
       && ix86_function_regparm (TREE_TYPE (decl), NULL) >= 3)
     return false;
 
+  /* If we need to align the outgoing stack, then sibcalling would
+     unalign the stack, which may break the called function.  */
+  if (ix86_incoming_stack_boundary < PREFERRED_STACK_BOUNDARY)
+    return false;
+
   /* Otherwise okay.  That also includes certain types of indirect calls.  */
   return true;
 }
@@ -7770,6 +7775,11 @@ ix86_update_stack_boundary (void)
                           TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))))
     ix86_incoming_stack_boundary = MIN_STACK_BOUNDARY;
 
+  /* The incoming stack frame has to be aligned at least at
+     parm_stack_boundary.  */
+  if (ix86_incoming_stack_boundary < crtl->parm_stack_boundary)
+    ix86_incoming_stack_boundary = crtl->parm_stack_boundary;
+
   /* Stack at entrance of main is aligned by runtime.  We use the
      smallest incoming stack boundary. */
   if (ix86_incoming_stack_boundary > MAIN_STACK_BOUNDARY
index d3c09d1ecaac649189381f26c792b79056d8d7ce..1548eedc7755f31e2f37b04959b5b1240e4f2442 100644 (file)
@@ -1,3 +1,11 @@
+2008-11-25  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR middle-end/37843
+       * gcc.target/i386/align-main-3.c: New.
+       * gcc.target/i386/pr37843-1.c: Likewise.
+       * gcc.target/i386/pr37843-2.c: Likewise.
+       * gcc.target/i386/pr37843-3.c: Likewise.
+
 2008-11-25  Mikael Morin  <mikael.morin@tele2.fr>
 
        PR fortran/36463
diff --git a/gcc/testsuite/gcc.target/i386/align-main-3.c b/gcc/testsuite/gcc.target/i386/align-main-3.c
new file mode 100644 (file)
index 0000000..d2f88d8
--- /dev/null
@@ -0,0 +1,14 @@
+/* Test for stack alignment with sibcall optimization.  */
+/* { dg-do compile { target { *-*-linux* && ilp32 } } } */
+/* { dg-options "-O2 -mpreferred-stack-boundary=4 -mincoming-stack-boundary=2" } */
+/* { dg-final { scan-assembler "andl\[\\t \]*\\$-16,\[\\t \]*%\[re\]?sp" } } */
+/* { dg-final { scan-assembler "call\[\\t \]*foo" } } */
+/* { dg-final { scan-assembler-not "jmp\[\\t \]*foo" } } */
+
+extern int foo (void);
+
+int
+main ()
+{
+  return foo ();
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr37843-1.c b/gcc/testsuite/gcc.target/i386/pr37843-1.c
new file mode 100644 (file)
index 0000000..12b70fd
--- /dev/null
@@ -0,0 +1,12 @@
+/* Test for stack alignment with sibcall optimization.  */
+/* { dg-options "-O2 -mpreferred-stack-boundary=6 -mincoming-stack-boundary=5" } */
+/* { dg-final { scan-assembler "and\[lq\]?\[\\t \]*\\$-64,\[\\t \]*%\[re\]?sp" } } */
+/* { dg-final { scan-assembler "call\[\\t \]*foo" } } */
+/* { dg-final { scan-assembler-not "jmp\[\\t \]*foo" } } */
+
+extern int foo (void);
+
+int bar (void)
+{
+    return foo();
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr37843-2.c b/gcc/testsuite/gcc.target/i386/pr37843-2.c
new file mode 100644 (file)
index 0000000..6b26d0d
--- /dev/null
@@ -0,0 +1,12 @@
+/* Test for stack alignment with sibcall optimization.  */
+/* { dg-options "-O2 -mpreferred-stack-boundary=6 -mincoming-stack-boundary=6" } */
+/* { dg-final { scan-assembler-not "and\[lq\]?\[\\t \]*\\$-64,\[\\t \]*%\[re\]?sp" } } */
+/* { dg-final { scan-assembler-not "call\[\\t \]*foo" } } */
+/* { dg-final { scan-assembler "jmp\[\\t \]*foo" } } */
+
+extern int foo (void);
+
+int bar (void)
+{
+    return foo();
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr37843-3.c b/gcc/testsuite/gcc.target/i386/pr37843-3.c
new file mode 100644 (file)
index 0000000..7923b29
--- /dev/null
@@ -0,0 +1,13 @@
+/* Test for stack alignment with sibcall optimization.  */
+/* { dg-do compile { target { *-*-linux* && ilp32 } } } */
+/* { dg-options "-O2 -std=gnu99 -mpreferred-stack-boundary=4 -mincoming-stack-boundary=2" } */
+/* { dg-final { scan-assembler-not "andl\[\\t \]*\\$-16,\[\\t \]*%\[re\]?sp" } } */
+/* { dg-final { scan-assembler-not "call\[\\t \]*foo" } } */
+/* { dg-final { scan-assembler "jmp\[\\t \]*foo" } } */
+
+extern int foo (_Decimal128);
+
+int bar (_Decimal128 x)
+{
+    return foo(x);
+}