This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Arranging for -Os to imply -mpreferred-stack-boundary=2 on x86


One of our customers complained about the additional code generated to
maintain the stack aligned to 16-byte boundaries, and suggested us to
default to the minimum alignment when optimizing for code size.  This
has the caveat that, when you link code optimized for size with code
optimized for speed, if a function optimized for size calls a
performance-critical function with the stack misaligned, the
performance-critical function may perform poorly.

I'm considering introducing a function attribute to specify the
alignment requirements of a given function, with similar effects to
RTH's FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN, so that one can control
performance-critical (re-)entry points that would benefit from
additional alignment.  I suppose this would address most of the
concerns discussed in the long thread starting at
http://gcc.gnu.org/ml/gcc/2001-07/msg00404.html.  Comments?

Meanwhile, is this ok to install?  Build- and hand-tested on
athlon-pc-linux-gnu.

Index: gcc/ChangeLog
from  Alexandre Oliva  <aoliva@redhat.com>

	* config/i386/i386.c (override_options): Default to minimum
	stack alignment when optimizing for code size.
	* doc/invoke.texi (-mpreferred-stack-boundary): Document the
	change.

Index: gcc/config/i386/i386.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/config/i386/i386.c,v
retrieving revision 1.323
diff -u -p -r1.323 i386.c
--- gcc/config/i386/i386.c 2001/10/22 07:19:01 1.323
+++ gcc/config/i386/i386.c 2001/10/23 09:42:41
@@ -973,8 +973,12 @@ override_options ()
     align_functions = 1 << abs (processor_target_table[ix86_cpu].align_func);
 
   /* Validate -mpreferred-stack-boundary= value, or provide default.
-     The default of 128 bits is for Pentium III's SSE __m128.  */
-  ix86_preferred_stack_boundary = 128;
+     The default of 128 bits is for Pentium III's SSE __m128, but we
+     don't want additional code to keep the stack aligned when
+     optimizing for code size.  */
+  ix86_preferred_stack_boundary = (optimize_size
+				   ? TARGET_64BIT ? 64 : 32
+				   : 128);
   if (ix86_preferred_stack_boundary_string)
     {
       i = atoi (ix86_preferred_stack_boundary_string);
Index: gcc/doc/invoke.texi
===================================================================
RCS file: /cvs/gcc/egcs/gcc/doc/invoke.texi,v
retrieving revision 1.65
diff -u -p -r1.65 invoke.texi
--- gcc/doc/invoke.texi 2001/10/22 07:42:23 1.65
+++ gcc/doc/invoke.texi 2001/10/23 09:42:42
@@ -7587,12 +7587,13 @@ startup modules.
 @opindex mpreferred-stack-boundary
 Attempt to keep the stack boundary aligned to a 2 raised to @var{num}
 byte boundary.  If @option{-mpreferred-stack-boundary} is not specified,
-the default is 4 (16 bytes or 128 bits).
+the default is 4 (16 bytes or 128 bits), except when optimizing for code
+size (@option{-Os}), in which case the default is the minimum correct
+alignment (4 bytes for x86, and 8 bytes for x86-64).
 
-The stack is required to be aligned on a 4 byte boundary.  On Pentium
-and PentiumPro, @code{double} and @code{long double} values should be
-aligned to an 8 byte boundary (see @option{-malign-double}) or suffer
-significant run time performance penalties.  On Pentium III, the
+On Pentium and PentiumPro, @code{double} and @code{long double} values
+should be aligned to an 8 byte boundary (see @option{-malign-double}) or
+suffer significant run time performance penalties.  On Pentium III, the
 Streaming SIMD Extension (SSE) data type @code{__m128} suffers similar
 penalties if it is not 16 byte aligned.
 
@@ -7604,10 +7605,10 @@ stack boundary from a function compiled 
 boundary will most likely misalign the stack.  It is recommended that
 libraries that use callbacks always use the default setting.
 
-This extra alignment does consume extra stack space.  Code that is sensitive
-to stack space usage, such as embedded systems and operating system kernels,
-may want to reduce the preferred alignment to
-@option{-mpreferred-stack-boundary=2}.
+This extra alignment does consume extra stack space, and generally
+increases code size.  Code that is sensitive to stack space usage, such
+as embedded systems and operating system kernels, may want to reduce the
+preferred alignment to @option{-mpreferred-stack-boundary=2}.
 
 @item -mmmx
 @itemx -mno-mmx

-- 
Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer                  aoliva@{cygnus.com, redhat.com}
CS PhD student at IC-Unicamp        oliva@{lsd.ic.unicamp.br, gnu.org}
Free Software Evangelist    *Please* write to mailing lists, not to me

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]