[llvm] Simplify `(a % b) lt/ge (b-1)` into `(a % b) eq/ne (b-1)` (PR #72504)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 27 11:33:39 PST 2023


dtcxzyw wrote:

> @dtcxzyw I tried to handle negative divisors, but the code doesn't; change the tests:
> 
> ```
> define i1 @srem_sgt_test(i16 %x) {
> ; CHECK-LABEL: @srem_sgt_test(
> ; CHECK-NEXT:    [[Y:%.*]] = srem i16 [[X:%.*]], 2259
> ; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i16 [[Y]], -2258
> ; CHECK-NEXT:    ret i1 [[CMP]]
> ;
>   %Cplus1 = add i16 -2259, 1
>   %y = srem i16 %x, -2259
>   %cmp = icmp sgt i16 %y, %Cplus1
>   ret i1 %cmp
> }
> ```

You should match the constant rhs of icmp directly instead of matching the add inst.
```
const APInt *C1, *C2;
if (match(&I, m_ICmp(Pred, m_SRem(m_Value(X), m_Negative(C1)), m_APInt(C2))) {
    if (Pred == ICmpInst::ICMP_SGT && *C2 == *C1 + 1) {
        // do the folding... 
    }
}
```



https://github.com/llvm/llvm-project/pull/72504


More information about the llvm-commits mailing list