PR target/82166: Update preferred stack boundary for leaf functions

Message ID 20170911015726.GA24758@gmail.com
State New
Headers show
Series
  • PR target/82166: Update preferred stack boundary for leaf functions
Related show

Commit Message

H.J. Lu Sept. 11, 2017, 1:57 a.m.
preferred_stack_boundary may not be the minimum stack alignment.  For
leaf functions without TLS access, max_used_stack_slot_alignment may be
smaller.  We should update preferred_stack_boundary for leaf functions.

Tested on i686 and x86-64.  OK for trunk?

H.J.
---
gcc/

	PR target/82166
	* config/i386/i386.c (ix86_finalize_stack_frame_flags): Properly
	compute the minimum stack alignment.  Also update preferred stack
	boundary when main () is a leaf function.

gcc/testsuite/

	PR target/82166
	* gcc.target/i386/pr82166.c: New test.
---
 gcc/config/i386/i386.c                  | 17 ++++++++++++-----
 gcc/testsuite/gcc.target/i386/pr82166.c | 14 ++++++++++++++
 2 files changed, 26 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/i386/pr82166.c

Comments

Uros Bizjak Sept. 17, 2017, 3:44 p.m. | #1
On Mon, Sep 11, 2017 at 3:57 AM, H.J. Lu <hjl.tools@gmail.com> wrote:
> preferred_stack_boundary may not be the minimum stack alignment.  For
> leaf functions without TLS access, max_used_stack_slot_alignment may be
> smaller.  We should update preferred_stack_boundary for leaf functions.
>
> Tested on i686 and x86-64.  OK for trunk?
>
> H.J.
> ---
> gcc/
>
>         PR target/82166
>         * config/i386/i386.c (ix86_finalize_stack_frame_flags): Properly
>         compute the minimum stack alignment.  Also update preferred stack
>         boundary when main () is a leaf function.
>
> gcc/testsuite/
>
>         PR target/82166
>         * gcc.target/i386/pr82166.c: New test.

LGTM.

Thanks,
Uros.

> ---
>  gcc/config/i386/i386.c                  | 17 ++++++++++++-----
>  gcc/testsuite/gcc.target/i386/pr82166.c | 14 ++++++++++++++
>  2 files changed, 26 insertions(+), 5 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.target/i386/pr82166.c
>
> diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c
> index 4e93cc1043b..03338e422be 100644
> --- a/gcc/config/i386/i386.c
> +++ b/gcc/config/i386/i386.c
> @@ -14257,11 +14257,12 @@ ix86_finalize_stack_frame_flags (void)
>    unsigned int incoming_stack_boundary
>      = (crtl->parm_stack_boundary > ix86_incoming_stack_boundary
>         ? crtl->parm_stack_boundary : ix86_incoming_stack_boundary);
> +  unsigned int stack_alignment
> +    = (crtl->is_leaf && !ix86_current_function_calls_tls_descriptor
> +       ? crtl->max_used_stack_slot_alignment
> +       : crtl->stack_alignment_needed);
>    unsigned int stack_realign
> -    = (incoming_stack_boundary
> -       < (crtl->is_leaf && !ix86_current_function_calls_tls_descriptor
> -         ? crtl->max_used_stack_slot_alignment
> -         : crtl->stack_alignment_needed));
> +    = (incoming_stack_boundary < stack_alignment);
>    bool recompute_frame_layout_p = false;
>
>    if (crtl->stack_realign_finalized)
> @@ -14306,7 +14307,9 @@ ix86_finalize_stack_frame_flags (void)
>                            HARD_FRAME_POINTER_REGNUM);
>
>        /* The preferred stack alignment is the minimum stack alignment.  */
> -      unsigned int stack_alignment = crtl->preferred_stack_boundary;
> +      if (stack_alignment > crtl->preferred_stack_boundary)
> +       stack_alignment = crtl->preferred_stack_boundary;
> +
>        bool require_stack_frame = false;
>
>        FOR_EACH_BB_FN (bb, cfun)
> @@ -14349,6 +14352,10 @@ ix86_finalize_stack_frame_flags (void)
>                 = incoming_stack_boundary;
>               crtl->stack_alignment_needed
>                 = incoming_stack_boundary;
> +             /* Also update preferred_stack_boundary for leaf
> +                functions.  */
> +             crtl->preferred_stack_boundary
> +               = incoming_stack_boundary;
>             }
>         }
>        else
> diff --git a/gcc/testsuite/gcc.target/i386/pr82166.c b/gcc/testsuite/gcc.target/i386/pr82166.c
> new file mode 100644
> index 00000000000..8bc63e15231
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/i386/pr82166.c
> @@ -0,0 +1,14 @@
> +/* { dg-do compile } */
> +/* { dg-options "-Os" } */
> +
> +void foo (void);
> +int a, b, c;
> +int
> +main (void)
> +{
> +  int j;
> +  for (; c;)
> +    a = b;
> +  for (; j;)
> +    foo ();
> +}
> --
> 2.13.5
>

Patch

diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c
index 4e93cc1043b..03338e422be 100644
--- a/gcc/config/i386/i386.c
+++ b/gcc/config/i386/i386.c
@@ -14257,11 +14257,12 @@  ix86_finalize_stack_frame_flags (void)
   unsigned int incoming_stack_boundary
     = (crtl->parm_stack_boundary > ix86_incoming_stack_boundary
        ? crtl->parm_stack_boundary : ix86_incoming_stack_boundary);
+  unsigned int stack_alignment
+    = (crtl->is_leaf && !ix86_current_function_calls_tls_descriptor
+       ? crtl->max_used_stack_slot_alignment
+       : crtl->stack_alignment_needed);
   unsigned int stack_realign
-    = (incoming_stack_boundary
-       < (crtl->is_leaf && !ix86_current_function_calls_tls_descriptor
-	  ? crtl->max_used_stack_slot_alignment
-	  : crtl->stack_alignment_needed));
+    = (incoming_stack_boundary < stack_alignment);
   bool recompute_frame_layout_p = false;
 
   if (crtl->stack_realign_finalized)
@@ -14306,7 +14307,9 @@  ix86_finalize_stack_frame_flags (void)
 			   HARD_FRAME_POINTER_REGNUM);
 
       /* The preferred stack alignment is the minimum stack alignment.  */
-      unsigned int stack_alignment = crtl->preferred_stack_boundary;
+      if (stack_alignment > crtl->preferred_stack_boundary)
+	stack_alignment = crtl->preferred_stack_boundary;
+
       bool require_stack_frame = false;
 
       FOR_EACH_BB_FN (bb, cfun)
@@ -14349,6 +14352,10 @@  ix86_finalize_stack_frame_flags (void)
 		= incoming_stack_boundary;
 	      crtl->stack_alignment_needed
 		= incoming_stack_boundary;
+	      /* Also update preferred_stack_boundary for leaf
+	         functions.  */
+	      crtl->preferred_stack_boundary
+		= incoming_stack_boundary;
 	    }
 	}
       else
diff --git a/gcc/testsuite/gcc.target/i386/pr82166.c b/gcc/testsuite/gcc.target/i386/pr82166.c
new file mode 100644
index 00000000000..8bc63e15231
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr82166.c
@@ -0,0 +1,14 @@ 
+/* { dg-do compile } */
+/* { dg-options "-Os" } */
+
+void foo (void);
+int a, b, c;
+int
+main (void)
+{
+  int j;
+  for (; c;)
+    a = b;
+  for (; j;)
+    foo ();
+}