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

    <tr>
        <th>Summary</th>
        <td>
            Opt should know `x/2 <= x - x/2` is true for `udiv`
        </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>
    Today, this doesn't optimize away <https://llvm.godbolt.org/z/jPjo6M858>
```llvm
define noundef zeroext i1 @demo(i32 noundef %x) unnamed_addr #0 {
start:
  %_2 = udiv i32 %x, 2
  %_3 = sub i32 %x, %_2
  %_0 = icmp ule i32 %_2, %_3
  ret i1 %_0
}
```
but that could just be `return true`, as proven in <https://alive2.llvm.org/ce/z/dxHhft>.

Indeed, it's always `true` for any denominator greater than zero.  Proof: <https://alive2.llvm.org/ce/z/CmkN_m>

This is important for "half but it needs to be rounded up" cases.  `x/2 <= x` is already understood to be true, but when you need the bigger half instead, the optimizer is more confused.  (I lost the original non-minimized example, sorry, but it was something like splitting an n-element slice into `n/2` and `n-n/2` slices and ending up with the bounds checking not optimizing out.)

---

Maybe start by getting `sub nuw` for this? Like
```diff
   %_2 = udiv i32 %x, 2
-  %_3 = sub i32 %x, %_2
+ %_3 = sub nuw i32 %x, %_2
```
Proof for that narrower part: <https://alive2.llvm.org/ce/z/xoCDe5>

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVcFu2zgQ_Rr6MrAhUZYlH3RonRpbYLvbQ-8BJY4kJhQpkMPYztcvSNlukmKBFjBkjPRm-B5nHim8V4NBbFj5mZUPKxFotK7xnSWaumnVWnlpflgpLowfgEblQVr0hvGKwM6kJvWKIE7iAqw4jESzZ8Unxo-MH7V-mTaDla3VtLFuYPz4yvjx6fuT3X2ry5oVX1j2wLJPbJctv5ixvJLYK4NgbDASe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