Function (or more narrow) scope static variables (as well as others not placed on the stack) should also not have any effect on the stack alignment. I noticed the issue first with Linux'es dynamic_pr_debug() construct using an 8-byte aligned sub-file-scope local variable.
According to my checking bad behavior started with 4.6.x (4.5.3 was still okay), but generated code got quite a bit worse as of 4.9.0. [v4: Bail early, using is_global_var(), as requested by Bernd.] [v3: Re-base to current trunk.] [v2: Drop inclusion of hard register variables, as requested by Jakub and Richard.] gcc/ 2015-12-11 Jan Beulich <jbeul...@suse.com> * cfgexpand.c (expand_one_var): Exit early for static and external variables when adjusting stack alignment related. gcc/testsuite/ 2015-12-11 Jan Beulich <jbeul...@suse.com> * gcc.c-torture/execute/stkalign.c: New. --- 2015-12-09/gcc/cfgexpand.c +++ 2015-12-09/gcc/cfgexpand.c @@ -1550,6 +1550,9 @@ expand_one_var (tree var, bool toplevel, if (TREE_TYPE (var) != error_mark_node && TREE_CODE (var) == VAR_DECL) { + if (is_global_var (var)) + return 0; + /* Because we don't know if VAR will be in register or on stack, we conservatively assume it will be on stack even if VAR is eventually put into register after RA pass. For non-automatic --- 2015-12-09/gcc/testsuite/gcc.c-torture/execute/stkalign.c +++ 2015-12-09/gcc/testsuite/gcc.c-torture/execute/stkalign.c @@ -0,0 +1,26 @@ +/* { dg-options "-fno-inline" } */ + +#include <assert.h> + +#define ALIGNMENT 64 + +unsigned test(unsigned n, unsigned p) +{ + static struct { char __attribute__((__aligned__(ALIGNMENT))) c; } s; + unsigned x; + + assert(__alignof__(s) == ALIGNMENT); + asm ("" : "=g" (x), "+m" (s) : "0" (&x)); + + return n ? test(n - 1, x) : (x ^ p); +} + +int main (int argc, char *argv[] __attribute__((unused))) +{ + unsigned int x = test(argc, 0); + + x |= test(argc + 1, 0); + x |= test(argc + 2, 0); + + return !(x & (ALIGNMENT - 1)); +}
avoid alignment of static variables affecting stack's Function (or more narrow) scope static variables (as well as others not placed on the stack) should also not have any effect on the stack alignment. I noticed the issue first with Linux'es dynamic_pr_debug() construct using an 8-byte aligned sub-file-scope local variable. According to my checking bad behavior started with 4.6.x (4.5.3 was still okay), but generated code got quite a bit worse as of 4.9.0. [v4: Bail early, using is_global_var(), as requested by Bernd.] [v3: Re-base to current trunk.] [v2: Drop inclusion of hard register variables, as requested by Jakub and Richard.] gcc/ 2015-12-11 Jan Beulich <jbeul...@suse.com> * cfgexpand.c (expand_one_var): Exit early for static and external variables when adjusting stack alignment related. gcc/testsuite/ 2015-12-11 Jan Beulich <jbeul...@suse.com> * gcc.c-torture/execute/stkalign.c: New. --- 2015-12-09/gcc/cfgexpand.c +++ 2015-12-09/gcc/cfgexpand.c @@ -1550,6 +1550,9 @@ expand_one_var (tree var, bool toplevel, if (TREE_TYPE (var) != error_mark_node && TREE_CODE (var) == VAR_DECL) { + if (is_global_var (var)) + return 0; + /* Because we don't know if VAR will be in register or on stack, we conservatively assume it will be on stack even if VAR is eventually put into register after RA pass. For non-automatic --- 2015-12-09/gcc/testsuite/gcc.c-torture/execute/stkalign.c +++ 2015-12-09/gcc/testsuite/gcc.c-torture/execute/stkalign.c @@ -0,0 +1,26 @@ +/* { dg-options "-fno-inline" } */ + +#include <assert.h> + +#define ALIGNMENT 64 + +unsigned test(unsigned n, unsigned p) +{ + static struct { char __attribute__((__aligned__(ALIGNMENT))) c; } s; + unsigned x; + + assert(__alignof__(s) == ALIGNMENT); + asm ("" : "=g" (x), "+m" (s) : "0" (&x)); + + return n ? test(n - 1, x) : (x ^ p); +} + +int main (int argc, char *argv[] __attribute__((unused))) +{ + unsigned int x = test(argc, 0); + + x |= test(argc + 1, 0); + x |= test(argc + 2, 0); + + return !(x & (ALIGNMENT - 1)); +}