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

            Bug ID: 111626
           Summary: missed optimization combining offset of array member
                    in struct with offset inside the array
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lis8215 at gmail dot com
  Target Milestone: ---

The simple code:

struct some_struct {
    uint32_t some_member;
    uint32_t arr[4][16];
};

uint32_t fn(const struct some_struct *arr, int idx)
{
    return arr->arr[1][idx];
}

is used to showcase a suboptimal optimization on some platforms including
RISC-V and MIPS (32 & 64 bit) even with `some_member` commented out.

while GCC emits:
        addi    a1,a1,16
        slli    a1,a1,2
        add     a0,a0,a1
        lw      a0,4(a0)
        ret

Clang does better job:
        slli    a1, a1, 2
        add     a0, a0, a1
        lw      a0, 68(a0)
        ret

Reply via email to