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

            Bug ID: 118235
           Summary: generate strange code to access const, but not static
                    array with doubles via index
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dushistov at mail dot ru
  Target Milestone: ---

Online demo available here https://godbolt.org/z/6jdKK1K7n .

For code like this:

```
enum class MyEnum { A, B, C, D };

double  f(MyEnum mode)
{
    const double factors[] = { 2.0, 1.5, 0.5, 0.67 };
    return factors[static_cast<unsigned int>(mode)];
}
```

gcc (trunk and all previous versions) on x86-64 generates really strange code:

```
f(MyEnum):
        movapd  xmm0, XMMWORD PTR .LC0[rip]
        mov     edi, edi
        movaps  XMMWORD PTR [rsp-40], xmm0
        movapd  xmm0, XMMWORD PTR .LC1[rip]
        movaps  XMMWORD PTR [rsp-24], xmm0
        movsd   xmm0, QWORD PTR [rsp-40+rdi*8]
        ret
.LC0:
        .long   0
        .long   1073741824
        .long   0
        .long   1073217536
.LC1:
        .long   0
        .long   1071644672
        .long   -687194767
        .long   1072001187
```

while clang generates:

```
f(MyEnum):
        mov     eax, edi
        lea     rcx, [rip + .L__const.f(MyEnum).factors]
        movsd   xmm0, qword ptr [rcx + 8*rax]
        ret

.L__const.f(MyEnum).factors:
        .quad   0x4000000000000000
        .quad   0x3ff8000000000000
        .quad   0x3fe0000000000000
        .quad   0x3fe570a3d70a3d71
```

gcc generates almost the same code as clang if mark `factors` array with
`static` keyword.

Reply via email to