[llvm] [InstCombine] Fold (X - X%C) u>= C to X u>= C (PR #214381)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 8 11:54:34 PDT 2026


================
@@ -3024,6 +3024,15 @@ Instruction *InstCombinerImpl::foldICmpSubConstant(ICmpInst &Cmp,
   ICmpInst::Predicate Pred = Cmp.getPredicate();
   Type *Ty = Sub->getType();
 
+  // D: (X - (X urem D)) is D*(X/D), a multiple of D, so it is >= D exactly when
+  // X >= D.
+  //   (icmp ugt (sub X, (urem X, D)), D-1) --> (icmp ugt X, D-1)
+  const APInt *D;
+  if (Pred == ICmpInst::ICMP_UGT &&
----------------
dtcxzyw wrote:

Please also handle the inverted case `icmp ult (sub X, (urem X, D)), C -> icmp ult X, D`. https://alive2.llvm.org/ce/z/x4vfAz


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


More information about the llvm-commits mailing list