https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78399

            Bug ID: 78399
           Summary: g++ generates sub-optimal assembler code when structs
                    aren't explicitly aligned.
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lucanus81 at gmail dot com
  Target Milestone: ---

Created attachment 40070
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=40070&action=edit
suboptimal code

Consider the following example:

struct pod { char x[7]; };
pod copy_pod(pod d) { return d; }

gcc (at any optimization level) will general sub-optimal assembler code (see
attachment).
The interesting thing is that if we change "pod" to contain a number of bytes
that fit into a cpu register (2, 4, 8, 16, 32, 64) then the generated assembler
is optimal (see attachment)

struct pod { char x[8]; };
pod copy_pod(pod d) { return d; }

One workaround I found is to explicitly use alignas:
struct alignas(8) pod { char x[7]; }; 

I'm wondering whether gcc should generate optimal code even without alignas(8).

Reply via email to