Issue 91527
Summary Opt should know `x/2 <= x - x/2` is true for `udiv`
Labels new issue
Assignees
Reporter scottmcm
    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>

_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to