[llvm] [InstCombine] fold unsigned predicates on srem result (PR #122520)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Sun Jan 12 03:40:15 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)->
+
+ const APInt *DivisorC;
+ if (!match(SRem->getOperand(1), m_APInt(DivisorC)))
+ return nullptr;
+
+ APInt NormalizedC = C;
+ if (Pred == ICmpInst::ICMP_ULT) {
+ assert(!NormalizedC.isZero() &&
+ "ult X, 0 should have been simplified already.");
+ --NormalizedC;
+ }
+ if (C.isNegative())
+ NormalizedC.flipAllBits();
+ assert(!DivisorC->isZero() &&
+ "srem X, 0 should have been simplified already.");
+ if (!NormalizedC.uge(DivisorC->abs() - 1))
----------------
dtcxzyw wrote:
```suggestion
if (!NormalizedC.ugt(DivisorC->abs()))
```
Proof: https://alive2.llvm.org/ce/z/mmxS8u
https://github.com/llvm/llvm-project/pull/122520
More information about the llvm-commits
mailing list