[llvm] [InstCombine] fold unsigned predicates on srem result (PR #122520)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Jan 12 08:13:02 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
----------------
goldsteinn wrote:
nit: For the comment can you be consistent w/ either ~C syntax or ~IR syntax i.e
`(X % DivisorC) ugt C -> (X % DivisorC) slt 0)` -> `(icmp ugt (srem X, DivisorC), C) -> (icmp slt (srem X, DivisorC), 0)` or `(X s% DivisorC) u> C` -> `(X s% DivisorC) s< 0`
https://github.com/llvm/llvm-project/pull/122520
More information about the llvm-commits
mailing list