gcc version: 4.2.1 (also occurs with versions as early as 4.0.2)
configured with: ../gcc-4.2.1/configure --enable-languages=c,c++

With gcc 4.x, local variable arrays declared on the stack are no longer aligned
at word boundaries. For example:

void somefunc()
{
...
  char buf[100];
...
}

In gcc 3.x, buf would be aligned on a word boundary. With gcc 4.x, the
alignment assigned is that of the underlying type - in this case 1-byte for
char array.  

The cause of the change in alignment seems to be the following:
- gcc 4.x allocates stack in cfgexpand.c, function expand_one_stack_var()
- this calls get_decl_align_unit(), also in cfgexpand.c
- the alignment is then read using macro DECL_ALIGN, which returns the
alignment of the type
- a further revision is made using macro LOCAL_ALIGNMENT
- however, LOCAL_ALIGNMENT is not defined for the sparc architecture (in
gcc/config/sparc.h), causing the alignment to remain as default

In contrast, gcc 3.4.6 seems to allocate stack in function.c,
assign_stack_local_1(), which does not use the alignment macros

Why this is a problem:
- on the sparc platform, a cast from a stack allocated buffer to a structure
pointer fails due to the new alignment. Although such code is inherently
non-portable, there is a lot of it out there which used to work with the 3.x
family.
- there is also a small performance loss from accessing unaligned structures.
This is seen in software that receives data from external sources (a socket for
example) into a stack buffer.

Suggested modification:
- add a LOCAL_ALIGNMENT macro for sparc and align arrays on word-boundaries, as
was done before

See Also:
- bug id 22605 reported against gcc 4.0.1
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=22605


-- 
           Summary: Local (stack) arrays not aligned on word boundaries
           Product: gcc
           Version: 4.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: amruth dot laxman at nsn dot com
 GCC build triplet: sparc-sun-solaris2.10
  GCC host triplet: sparc-sun-solaris2.10
GCC target triplet: sparc-sun-solaris2.10


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33594

Reply via email to