| Issue |
237772
|
| Summary |
[CVP] `X urem Y` is not expanded when the largest X is exactly 2*Y
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
julesreyn
|
`expandUDivOrURem` in CorrelatedValuePropagation turns `X urem Y` into a compare and a select when `X u< 2*Y`. The check is strict, so the `urem` stays when the largest X is exactly `2*Y`, although that one value is cheap to handle.
This happens with a sum of two bytes modulo 255, as in Fletcher-16: X is at most 255 + 255 = 510 = 2 * 255.
```llvm
define i16 @src(i8 noundef zeroext %a, i8 noundef zeroext %b) {
%x = zext i8 %a to i16
%y = zext i8 %b to i16
%s = add nuw nsw i16 %x, %y
%r = urem i16 %s, 255
ret i16 %r
}
define i16 @tgt(i8 noundef zeroext %a, i8 noundef zeroext %b) {
%x = zext i8 %a to i16
%y = zext i8 %b to i16
%s = add nuw nsw i16 %x, %y
%lt = icmp ult i16 %s, 255
%sub = add nsw i16 %s, -255
%sel = select i1 %lt, i16 %s, i16 %sub
%top = icmp eq i16 %s, 510
%r = select i1 %top, i16 0, i16 %sel
ret i16 %r
}
```
Alive2: https://alive2.llvm.org/ce/z/w3Z5A7
`opt -O3` trunk leaves the `urem` in `@src`. Full Fletcher-16 update on clang trunk: https://godbolt.org/z/1YW6eP536
Impact on this reduced case, estimated by llvm-mca (x86-64, `-mcpu=skylake`, LLVM b974545, 100 iterations): 8 to 8 instructions, 243 to 212 cycles. The gain is small, and I am not sure that the extra select is worth it in general.
The full function was measured with a different rewrite, so its timings are not evidence for this fold and are left out.
This came out of [qed-superopt](https://github.com/benoithms/qed-superopt), an AI-guided superoptimizer, which found a faster rewrite of the full function, proven with Alive2. The reduced case above was derived from it and proven separately. I reviewed all of it, checked the proof and, having worked on compiler projects, understand the fold.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs