[llvm] Fold (a % b) lt/ge (b-1) where b is a power of 2 (PR #72504)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Sun Nov 19 00:12:14 PST 2023
================
@@ -6837,6 +6837,22 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
Changed = true;
}
+ {
+ Value *X, *Y;
+ ICmpInst::Predicate Pred = I.getPredicate();
+ if ((Pred == ICmpInst::ICMP_SGE || Pred == ICmpInst::ICMP_SLT) &&
+ match(Op0, m_OneUse(m_SRem(m_Value(X), m_Value(Y)))) &&
+ match(Op1, m_OneUse(m_c_Add(m_Deferred(Y), m_AllOnes()))) &&
+ isKnownNonNegative(Y, DL, 0, Q.AC, Q.CxtI, Q.DT)) {
+ // icmp slt (X % C), (C - 1) --> icmp ne (X % C), (C - 1)
+ auto *NewCmp = Builder.CreateICmpNE(Op0, Op1);
+ // icmp sge (X % C), (C - 1) --> icmp eq (X % C), (C - 1)
+ if (Pred == ICmpInst::ICMP_SGE)
+ NewCmp = Builder.CreateICmpEQ(Op0, Op1);
+ return replaceInstUsesWith(I, NewCmp);
----------------
dtcxzyw wrote:
```suggestion
// icmp slt (X % C), (C - 1) --> icmp ne (X % C), (C - 1)
// icmp sge (X % C), (C - 1) --> icmp eq (X % C), (C - 1)
auto *NewCmp = Builder.CreateICmp(Pred == ICmpInst::ICMP_SLT ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ, Op0, Op1);
return replaceInstUsesWith(I, NewCmp);
```
Don't create instructions that may be unused.
https://github.com/llvm/llvm-project/pull/72504
More information about the llvm-commits
mailing list