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

via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 19 10:05:06 PST 2023


================
@@ -6837,6 +6837,25 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
     Changed = true;
   }
 
+  {
+    Value *X, *Y;
+    const APInt *C;
+    ICmpInst::Predicate Pred = I.getPredicate();
+    if ((Pred == ICmpInst::ICMP_SGE || Pred == ICmpInst::ICMP_SLT) &&
+        match(Op0, m_OneUse(m_SRem(m_Value(X), m_Value(Y)))) &&
+        ((match(Op1, m_OneUse(m_c_Add(m_Deferred(Y), m_AllOnes()))) &&
----------------
goldsteinn wrote:

You could also handle `sle` w/ `Y + -2` (instead of `slt` + `Y + -1`). Likewise in context of @dtcxzyw proofs, you could do `sge` w/ `Y + 2`.

You can also handle `urem` with `sge`/`slt`/`uge`/`ult`:
https://alive2.llvm.org/ce/z/9nBnWe

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


More information about the llvm-commits mailing list