[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
Fri Jan 12 11:16:56 PST 2024


================
@@ -6855,6 +6855,45 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
   if (Value *V = simplifyICmpInst(I.getPredicate(), Op0, Op1, Q))
     return replaceInstUsesWith(I, V);
 
+  {
+    Value *X;
+    const APInt *C1, *C2;
+    ICmpInst::Predicate Pred;
+    if ((match(&I, m_ICmp(Pred, m_SRem(m_Value(X), m_NonNegative(C1)),
+                          m_APInt(C2))) &&
+         ((Pred == ICmpInst::ICMP_SLT && *C2 == *C1 - 1) ||
+          (Pred == ICmpInst::ICMP_SGT && *C2 == *C1 - 2))) ||
+        (match(&I,
+               m_ICmp(Pred, m_SRem(m_Value(X), m_Negative(C1)), m_APInt(C2))) &&
+         ((Pred == ICmpInst::ICMP_SGT && *C2 == *C1 + 1) ||
+          (Pred == ICmpInst::ICMP_SLT && *C2 == *C1 + 2)))) {
+      // icmp slt (X s% C), (C - 1) --> icmp ne (X s% C), (C - 1), if C >= 0
+      // icmp sgt (X s% C), (C - 2) --> icmp eq (X s% C), (C - 1), if C >= 0
+      // icmp sgt (X s% C), (C + 1) --> icmp ne (X s% C), (C + 1), if C < 0
+      // icmp slt (X s% C), (C + 2) --> icmp eq (X s% C), (C + 1), if C < 0
+      return CmpInst::Create(Instruction::ICmp,
+                             ((Pred == ICmpInst::ICMP_SLT && *C2 == *C1 - 1) ||
+                              (Pred == ICmpInst::ICMP_SGT && *C2 == *C1 + 1))
+                                 ? ICmpInst::ICMP_NE
+                                 : ICmpInst::ICMP_EQ,
+                             Op0,
+                             ConstantInt::get(Op1->getType(), C1->isNegative()
+                                                                  ? *C1 + 1
+                                                                  : *C1 - 1));
+    }
+
+    if (match(&I, m_ICmp(Pred, m_URem(m_Value(X), m_APInt(C1)), m_APInt(C2))) &&
+        ((Pred == ICmpInst::ICMP_ULT && *C2 == *C1 - 1) ||
+         (Pred == ICmpInst::ICMP_UGT && *C2 == *C1 - 2))) {
----------------
dtcxzyw wrote:

> and I think we should add C2->uge(1) for ULT and for UGT as you suggested C2->ugt(1)
> 
> I am working on it now, should I put the check or the assertion

See the alive2 proof: https://alive2.llvm.org/ce/z/LMvAC6


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


More information about the llvm-commits mailing list