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

    <tr>
        <th>Summary</th>
        <td>
            Missed Optimization: Aligned Pointer Optimizations Can't Happen With Prefered OR Instead of ADD
        </td>
    </tr>

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

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

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

<pre>
    Given code such as:
```cpp
void src(u8 arr[], u8 offset, u16 value) {
    if ((offset & 1) != 0) {
 __builtin_unreachable();
    }
    arr[offset + 0] = value;
 arr[offset + 1] = value >> 8;
}

void tgt(u8 arr[], u8 offset, u16 value) {
    arr[offset + 0] = value;
    arr[offset + 1] = value >> 8;
}
```
When the offset isn't known to align and just addition is used, this can compile to a single halfword store -- Whenever the offset is known though, the addition is folded into an OR and the folding optimization (which seems to occur during target selection) is unable to be taken!
Notably not all targets have this optimization either. For example, ARMv6 doesn't, but ARMv7 does!!
A similar issue was presented previously (#69938) but afaik this is a different optimization entirely? As a pull request which possibly should've remedied that issue does not fix this one!
This issue does not persist with a constant offset.

Tested on Godbolt with Clang (trunk) and nightly Rustc
Alive2: https://alive2.llvm.org/ce/z/qjcLWd
Godbolt: https://godbolt.org/z/93jo9aKTx
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVE1v2zgQ_TX0ZRBDJmVLOujgxHV2sdttEQTIMaDEkTQJTar8cJL--gUlx43bPRQLBIhFvjfvzeOQ0nvqDWLN1tdsvVvIGAbr6lu0QWpcNFa91bd0RAOtVQg-tgNIz8SWZTuWbdkmm__acZxXjpYUeNcyXsYSpHNzYcZvIJZgu85jmD5WGzhKHZHxClhxPbMBAKgDxkvGyxkMjG9gNaH4iokdZJeMx8cmkg5kHqNxKNtBNhqnAhUTH8qyYvfjY_Z1FriGjK13kKrPns7EX4Cr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