<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/61800>61800</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Bad codegen when adding `const` keyword to array
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:X86,
            llvm:codegen
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          duk-37
      </td>
    </tr>
</table>

<pre>
    [compiler explorer link](https://godbolt.org/z/7b48vqjW3)

Code:
```cpp
#include <stdint.h>
#include <string.h>

#define ARRLEN(x) (sizeof(x) / sizeof(*x))

#define CONST const

CONST uint32_t array[] = {
 4473924, 2236962, 1118481, 559240,
    279620, 139810, 69905, 34952,
 17476, 8738, 4369, 0,
    0, 0, 0, 0
};

void test(uint32_t shift, float* a, float* b) {
    shift <<= 23;

    uint32_t dt_rA[ARRLEN(array)];
    memcpy(dt_rA, array, sizeof(array));

 for(int i = 0; i < ARRLEN(array); i++) {
        dt_rA[i] |= shift;
 }

    memcpy(a, dt_rA, sizeof(dt_rA));
}
```

Codegen w/o `const`:

```asm
test(unsigned int, float*, float*): # @test(unsigned int, float*, float*)
        shl     edi, 23
        vpbroadcastd    zmm0, edi
        vpord   zmm0, zmm0, zmmword ptr [rip + array]
        vmovdqu64       zmmword ptr [rsi], zmm0
        ret
```

Codegen w/ `const`:
```asm
test(unsigned int, float*, float*): # @test(unsigned int, float*, float*)
        vmovd   xmm0, edi
 shl     edi, 23
        lea     eax, [rdi + 17476]
        vmovd xmm1, edi
        vpbroadcastd    xmm1, xmm1
        vpbroadcastd ymm0, xmm0
        vpslld  ymm0, ymm0, 23
        vpor    ymm0, ymm0, ymmword ptr [rip + .LCPI0_0]
        vmovdqu ymmword ptr [rsi], ymm0
 vpor    xmm0, xmm1, xmmword ptr [rip + .LCPI0_1]
        vmovd xmm1, eax
        vpblendd        xmm0, xmm0, xmm1, 1             # xmm0 = xmm1[0],xmm0[1,2,3]
        vpinsrd xmm0, xmm0, edi, 3
        vmovdqa ymm1, ymmword ptr [rip + .LCPI0_2] # ymm1 = [0,1,2,3,3,3,3,3]
 vpermd  ymm0, ymm1, ymm0
        vmovdqu ymmword ptr [rsi + 32], ymm0
 ret
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVt2u4jYQfprhxlrkjPN7wQWBRaq02lbbSu3dyokNeE_-1jYs7NNXdkgg4Zye9q4oYGc845n5vvFgbow6NFKuIMoh2i74yR5bvRKnlw8sWRStuLqVsq07VUlN5KWrWi01qVTzAtEWMD1a2xlga8Ad4O7QiqKt7LLVB8DdT8BdUoTp-fu3PxlgBnQLdN3_blohnVkvimn_lF13kyBTTVmdhCTANsYK1djlEdjH15e1ag6Py4OSkHvVSLL-8uXTx8-A6QUwI4CpUT9lu78LdmSUAK6dcBbufa_Nr59__4OUbWPsJB8vPqnGMvxqCdeaX3tQCbAtgSTv9UgYJizDEHBDEFmcxeimQRCkYRq4aRRlGFLAzc2AEIJJFiP1eixLAz-Ls4xGbsLCLMK7epCESezkacJSN4Ysztw43ZIOouGnzyXZAssf8zq3ShArjQVMx-zMUe2tM9tXLbeAa8Inb4VHdUyZkN7CceWfLUE28-OUxv2F_arXEOUjbz2cmLmaYw_b1rIuuytg2lvg5gY8bu6EjraYzX3uWw2YqsYS5VmiwHI_3ZBn124JMPfPLDv3GWJWnvLEZ9njNAbs0J2lPCbgARyzGIO_SabBj9sMx2Z-sA6yIT8Ady1xZ8qXakzvp21qzE3dSwaWG98TBFHNhOPpPAO2JoCMQEj_o-EENnOs_CiF8keCTZfPXaFbLkpurHDvP-va16tTnym2WjwoPIw_3EpnNYEo16ojgPlwPrezTer2LL6f4vD2Pjc2yje92-4TUy3tvyTldU7-H2x4AAghlyeY3-Gpkrxf5he37MASyiPd96NXkXZugjfYnNI-KPrxbc3rLezLEz3nzlSVIKPGMD7XW6vdONe7vl5Gy0-b336hX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