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

    <tr>
        <th>Summary</th>
        <td>
            Missed optimization: eliminate indirect load of global constant array
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            llvm:optimizations,
            missed-optimization
      </td>
    </tr>

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

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

<pre>
    Alive2 proof: https://alive2.llvm.org/ce/z/hzUuua

### Motivating example 

```llvm
%struct = type { i32, ptr }

@names = internal unnamed_addr constant [3 x %struct] [%struct { i32 0, ptr @.str }, %struct { i32 1, ptr @.str.1 }, %struct { i32 2, ptr null }]
@.str = external constant [4 x i8]
@.str.1 = external constant [4 x i8]
@id2name = private unnamed_addr constant [3 x ptr] [ptr @names, ptr getelementptr inbounds ([3 x %struct], ptr @names, i64 0, i64 1), ptr getelementptr inbounds ([3 x %struct], ptr @names, i64 0, i64 2)], align 8

define ptr @src(i64 %id, ptr %arrayidx) #0 {
  %switch.gep = getelementptr [3 x ptr], ptr @id2name, i64 0, i64 %id
  %switch.load = load ptr, ptr %switch.gep, align 8
  %name = getelementptr i8, ptr %switch.load, i64 8
  ret ptr %name
}
```
can be folded to:
```llvm
define ptr @tgt(i64 %id, ptr %arrayidx) #0 {
  %name = getelementptr [3 x %struct], ptr @names, i64 0, i64 %id, i32 1
  ret ptr %name
}
```

### Real-world motivation

This snippet of IR is derived from [openssl/providers/implementations/ciphers/cipher_cts.c@ossl_cipher_cbc_cts_mode_name2id](https://github.com/openssl/openssl/blob/8cd3f34758b292e137ce112a09f566821549115d/providers/implementations/ciphers/cipher_cts.c#L89) (after O3 pipeline).
The example above is a reduced version. If you're interested in the original suboptimal IR and optimal IR, email me please.

**Let me know if you can confirm that it's an optimization opportunity, thanks.**
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.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