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

    <tr>
        <th>Summary</th>
        <td>
            Optimize out `sub`+`GEPi` when it's used in `ptr2int`
        </td>
    </tr>

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

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

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

<pre>
    It's not legal to optimize await pointer `p + (q - p)` in general (<https://alive2.llvm.org/ce/z/ro77id>), due to provenance.

However, when it's used in `ptrtoint`, and thus the provenance isn't relevant, it would be helpful to simplify this.
```llvm
define { ptr, i64 } @slice_iter_roundtrip_via_slice(ptr noundef nonnull %0, i64 noundef %1) unnamed_addr #0 {
start:
 %ptr_addr.i = ptrtoint ptr %0 to i64
  %byte_diff.i = sub i64 %1, %ptr_addr.i
  %_2 = insertvalue { ptr, i64 } poison, ptr %0, 0
  %_11 = getelementptr inbounds i8, ptr %0, i64 %byte_diff.i
  %end_addr_or_len = ptrtoint ptr %_11 to i64
  %_3 = insertvalue { ptr, i64 } %_2, i64 %end_addr_or_len, 1
  ret { ptr, i64 } %_3
}
```
down to just
```llvm
define { ptr, i64 } @slice_iter_roundtrip_via_slice(ptr noundef nonnull %0, i64 noundef %1) unnamed_addr #0 {
start:
  %_2 = insertvalue { ptr, i64 } poison, ptr %0, 0
  %_3 = insertvalue { ptr, i64 } %_2, i64 %1, 1
  ret { ptr, i64 } %_3
}
```
Alive proof: <https://alive2.llvm.org/ce/z/R327wi>

---

Context: I'd like this to improve the handling of slice iterators in Rust.

Today, this isn't a NOP in LLVM-IR (though it is in x86):
```rust
pub fn slice_iter_roundtrip_via_slice(it: std::slice::Iter<'_, i32>) -> std::slice::Iter<'_, i32> {
 it.as_slice().into_iter()
}
```
<https://rust.godbolt.org/z/vY3eMeqr3>

And while with the current implementation LLVM isn't allowed to do that, if this bug was fixed I could replace the `slice::Iter` implementation with something smarter about provenance that would turn it into the `{ ptr, i64 }` version seen in this issue, where the optimization would be legal.

(The NOP roundtrip by itself isn't that important, obviously, but it's the gateway to operating on iterators via the slice, rather than needing to repeat a bunch of helpers onto the iterator type too.)

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUVt-P2zYM_muUFyKBI-fi5CEP96PZDmjXoigG7CmQbTpWJ0uuRCVN__qBcpzm7tqtG_oy4HCxLYn8yO8TSRWC3lvEjbi5EzcPExWpdX4TKkfUVd2kdPVp80hCFgGsIzC4VwbIgetJd_oLgjoqTdA7bQk9iGXWg5B3IOTqE0yhF3ItlhloC3u06JXhFZHft0R9EPmtkFsht8roA8qZMYdu5vxeyG2FQm6_CLn1rih0LfJXbEneQx2R_ffeHdAqW-FMZA8iux3-_-qOeEDPG48tWtAD9BiwZgwMjzwxWLHMeJeyNVAbA1CLV0ZBBytkQeDR4EFZ4r2a4OiiqaFEaNH0TUypCLrrjW5OQK0OI5plNvxxSMOnGhttEURxBz0lhHq5AFE8gFhkwegKd5rQ77yLtiav-91Bq11aEHLVkwfLK9iAddZGw5m8yUY745qQN3Mh1xCtVR3WO1XXHoTMM3Y8AAmkPHHq0xuf6MmnjTMNIn-AMUX8kJxwlHq5OB_gT-WJcFfrpjkfCbEcwknu758avTq3k2m7tgE9HZSJ30xI73Rwlr-MCPg5u7YznydDeyQ02KEl3qltyWkIoFfPD5_BXeG-soZ2SNTO-Z1B-80ksMcXadjlPxJOivsKxDN3vDIfjXqk75nIz9IqHp5p7Kwvd7SM8GMM9L8T4U_Vxn8iZf6TaLjlUsaVxDUiv4V_U-re57I4ai51VxVtOp1ev947S_iZMwePQhY1GP0npsqT1NmlEpaKWatsbbTdg2sgEQjMrCLnA5fC9zHQk9L5wdXqxMEmY2P9U_Db23e8__Xr399MH99z-abWxX3L5VAnU59XSy7OI5mXhPiLEvtYQmPhHwWmU2CBajaW3w6f0-MjoRf5vZDFLhG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