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

    <tr>
        <th>Summary</th>
        <td>
            [InstCombine] sub(ptradd(p,x1),ptradd(p,x2)) not folded if multi-use
        </td>
    </tr>

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

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

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

<pre>
    https://llvm.godbolt.org/z/c55hdjoYe
```llvm
define i64 @src(ptr %p, i64 %idx1, i64 %idx2) {
  %p1 = getelementptr inbounds i8, ptr %p, i64 %idx1
  call void @use(ptr %p1)
  %p2 = getelementptr inbounds i8, ptr %p, i64 %idx2
  %p1.int = ptrtoint ptr %p1 to i64
 %p2.int = ptrtoint ptr %p2 to i64
  %sub = sub i64 %p1.int, %p2.int
 ret i64 %sub
}

declare void @use(ptr)
```

I'd expect this to fold down to a sub of idx1 and idx2, but currently this doesn't happen if one of the pointers has an extra use.

This is due to limitations in OptimizePointerDifference.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVMGOozgQ_RpzsYKggAAHDt2JIvVp97CXPRpcBPcaG9lFb7q_fmXDZpIZ9WWkSNjlqvee66UsvFdXg9ix6pVV50SsNFnXGfWPGpLeys9uIlo8K14YXBhctP6Y06uVvdWUWndlcPlicBmqapLv9m9k2ZllL-yYbb-QvoUkjsogV8eSszLzbmDQLOQ4g2phcNoOoFLylj9vgUHLWf26wfBYkHNWnPkVCTXOaCgAKdPb1UjPVRMAvsPeUQahNf-wSgY1q8cHNTmD9pEMfpcMniSnylBEWsiRDZs7IScbyvb0yPl9Njxnh5hf-5gbvjv9xhf03OH2Aof0f5Jf-92v-rwvdq8GLRz-2p97Z-4GP5a9Maglx9uCA3GalA9KR6sll_ZfEzYiSrQjD1ZwYSTfDD7xfiU-rM6hIf25FUuL3jCoiU9iWdBwNXJrMJTThHwJXUHn-SQ8F4bjjZzgq8f0UdNfASmArRgEaDUrEqSs8VwZ_sdCalZf-OeGdVbjiA7NgGkiu0K2RSsS7PI6hxrqtsiTqWvyITuKEWvRgsh6kNgem7auhqzpxxKzRHWQQZmVeZZVZVXkqWhyGGrR1mMzFvWxZ2WGs1A6jbNk3TVR3q_YNQ0UeaJFj9rHcQSI41O8KONpsHOvDDII7WIAs_Ie5cFuN4hXCmfVOXFdKDv069WzMtPKk_9BRYp0HPY34-m0Y1bxr7NZLKQMCwanWxwEOD0HIQZbbixFb1EGX-ZVkzqsHpPV6Z-ejKuiae3Twc77-7F_Douz7zgQg0tsgGdwiT34LwAA__-RuWN8">