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

    <tr>
        <th>Summary</th>
        <td>
            X86: Minor regression from llvm 14 with single array access and lea
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          SuperCoder7979
      </td>
    </tr>
</table>

<pre>
    ## Description
When arrays are accessed once in a function, llvm14 produces a mov instruction that references the array in memory. However, trunk produces a lea that moves the array into a register, then performs the mov. This change makes sense when the array is accessed multiple times as it removes the extraneous memory reference, but it adds an additional unneeded instruction when there is only a single access of the array.

## Code
Compiling with: `-O3`
Godbolt: https://godbolt.org/z/W4saM9fha

```cpp
constexpr const static int array[5] = {1, 2, 3, 4, 5};

int get(int a) {
    return array[a];
}

int get2(int a, int b) {
    return array[a] + array[b];
}
```

<details>
<summary>trunk codegen</summary>
<pre>
get(int):                                # @get(int)
        movsxd  rax, edi
        lea     rcx, [rip + array]
        mov     eax, dword ptr [rcx + 4*rax]
        ret
get2(int, int):                              # @get2(int, int)
        movsxd  rcx, edi
        lea     rdx, [rip + array]
        movsxd  rax, esi
        mov     eax, dword ptr [rdx + 4*rax]
        add     eax, dword ptr [rdx + 4*rcx]
        ret
</pre>
</details>
<details>
<summary>llvm 14 codegen</summary>
<pre>
get(int):
        movsxd  rax, edi
        mov     eax, dword ptr [4*rax + array]
        ret
get2(int, int):
        movsxd  rcx, edi
        movsxd  rax, esi
        mov     eax, dword ptr [4*rax + array]
        add     eax, dword ptr [4*rcx + array]
        ret
</pre>
</details>
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJylVkGPmzoQ_jVwsRqBCSQ5cNhNXttL9Q6t1F6NPYBfwUa22STv13cMCSS7qySrIrCNx_N55pvBQ6HFMQ9ogjfZgeVGdk5qFUS7IHr6WYM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