[llvm] [InstCombine]: Eliminate redundant modulus for urem (PR #157644)

via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 9 05:04:09 PDT 2025


================
@@ -2473,6 +2473,14 @@ Instruction *InstCombinerImpl::visitURem(BinaryOperator &I) {
     }
   }
 
+  Value *A;
+  Value *B;
+  // urem(urem(A, B), Op1) -> urem(A, Op1)
+  if (match(Op0, m_URem(m_Value(A), m_Value(B)))) {
----------------
kper wrote:

I have added another test for the general proof which was mentioned in the issue. However, `opt` won't remove the call to the `@llvm.assume`, so it looks like this

```
define i4 @fold_urem(i4 %a0, i4 %mod0, i4 %mod1) {
; CHECK-LABEL: @fold_urem(
; CHECK-NEXT:    [[V2:%.*]] = urem i4 [[A0:%.*]], [[MOD1:%.*]]
; CHECK-NEXT:    [[M:%.*]] = urem i4 [[MOD0:%.*]], [[MOD1]]
; CHECK-NEXT:    [[MZ:%.*]] = icmp eq i4 [[M]], 0
; CHECK-NEXT:    call void @llvm.assume(i1 [[MZ]])
; CHECK-NEXT:    ret i4 [[V2]]
;
  %v1 = urem i4 %a0, %mod0 ; this instruction should be eliminated
  %v2 = urem i4 %v1, %mod1
  %m = urem i4 %mod0, %mod1
  %mz = icmp eq i4 %m, 0
  call void @llvm.assume(i1 %mz)
  ret i4 %v2
}
```

https://alive2.llvm.org/ce/z/JbmCCR

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


More information about the llvm-commits mailing list