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

            Bug ID: 90616
           Summary: Suboptimal code generated for accessing an aligned
                    array.
           Product: gcc
           Version: 5.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: igusarov at mail dot ru
  Target Milestone: ---

Consider the following code:

    #include <stdint.h>

    uint8_t   globalArray[68]   __attribute__((aligned(256)));

    uint8_t foo(uint8_t index)
    {
        return globalArray[index];
    }

avr-gcc-5.4.0 generates the following code for the body of foo():

        mov    r30,r24    # r24 is where 'index' argument is stored
        ldi    r31,0
        subi   r30,lo8(-(globalArray))
        sbci   r31,hi8(-(globalArray))
        ld     r24,Z

which is suboptimal because the lower byte of the address of globalArray is
always 0 due to the extended alignment enforced on that object.

Would it be possible to generate a better code for such specific indexing? For
example, the following snippet takes full advantage of the overalignment:

        mov    r30,r24
        ldi    r31,hi8(globalArray)
        ld     r24,Z

Reply via email to