[llvm] [InstCombine] fold unsigned predicates on srem result (PR #122520)

Jacob Young via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 12 08:29:22 PST 2025


================
@@ -2674,10 +2674,41 @@ Instruction *InstCombinerImpl::foldICmpShrConstant(ICmpInst &Cmp,
 Instruction *InstCombinerImpl::foldICmpSRemConstant(ICmpInst &Cmp,
                                                     BinaryOperator *SRem,
                                                     const APInt &C) {
+  const ICmpInst::Predicate Pred = Cmp.getPredicate();
+  if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT) {
+    // Canonicalize unsigned predicates to signed:
+    // (X % DivisorC) ugt C -> (X % DivisorC) slt 0
+    //   iff (C slt 0 ? ~C : C) uge abs(DivisorC)-1
+    // (X % DivisorC) ult C+1 -> (X % DivisorC) sgt -1
+    //   iff (C+1 slt 0 ? ~C : C) uge abs(DivisorC)-1
----------------
jacobly0 wrote:

I was trying to copy the style of the nearby code, but I certainly wanted to write `s%` and `s<` so I'll do that.

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


More information about the llvm-commits mailing list