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

            Bug ID: 91616
           Summary: Incorrect data address computation in very simple code
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mpoulhies at kalray dot eu
  Target Milestone: ---

The following code is miscompiled in O2 only (0,1 and 3 are correct):

typedef unsigned long long uint64_t;

uint64_t data_array[16] = {0};

unsigned int i = 0;

int main(void) {
  unsigned long long seed = 0xcafebebedeadbeef;
  for (i = 0; i < 16; i++)
    data_array[i] = (seed++);

  return 0;
}

It emits:

main:
  movabs rax, -3819405706974609681
  movabs rsi, OFFSET FLAT:data_array-6338242491622225792 // <- this is not
correct
  movabs rcx, -3819405706974609665
.L2:
  mov rdx, rax
  add rax, 1
  mov QWORD PTR [rsi+rax*8], rdx
  cmp rax, rcx
  jne .L2
  mov DWORD PTR i[rip], 16
  xor eax, eax
  ret
i:
  .zero 4
data_array:
  .zero 128


After looking at compilers available on godbolt, all GCC starting from 8.* are
showing this behavior, see https://godbolt.org/z/OX0s10

The pass that seems to introduce this wrong address is 'ivopts' (first seen in
164t.ivopts in 9.2.1)

Reply via email to