Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify (a % b) lt/ge (b-1) into (a % b) eq/ne (b-1) #72504

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2519,6 +2519,30 @@ Instruction *InstCombinerImpl::foldICmpShrConstant(ICmpInst &Cmp,
Instruction *InstCombinerImpl::foldICmpSRemConstant(ICmpInst &Cmp,
BinaryOperator *SRem,
const APInt &C) {
{
const APInt *C1;
ICmpInst::Predicate Pred = Cmp.getPredicate();
if ((match(SRem->getOperand(1), m_NonNegative(C1))) &&
((Pred == ICmpInst::ICMP_SLT && C == *C1 - 1) ||
(Pred == ICmpInst::ICMP_SGT && C == *C1 - 2) ||
(Pred == ICmpInst::ICMP_SGT && C == -*C1 + 1) ||
(Pred == ICmpInst::ICMP_SLT && C == -*C1 + 2))) {
// icmp slt (X s% C), (C - 1) --> icmp ne (X s% C), (C - 1), if C >= 0
// icmp sgt (X s% C), (C - 2) --> icmp eq (X s% C), (C - 1), if C >= 0
// icmp sgt (X s% C), (-C + 1) --> icmp ne (X s% C), (-C + 1), if C >= 0
// icmp slt (X s% C), (-C + 2) --> icmp eq (X s% C), (-C + 1), if C >= 0
return new ICmpInst(
((Pred == ICmpInst::ICMP_SLT && C == *C1 - 1) ||
(Pred == ICmpInst::ICMP_SGT && C == -*C1 + 1))
? ICmpInst::ICMP_NE
: ICmpInst::ICMP_EQ,
SRem,
ConstantInt::get(SRem->getType(), C == -*C1 + 1 || C == -*C1 + 2
? -*C1 + 1
: *C1 - 1));
}
}

// Match an 'is positive' or 'is negative' comparison of remainder by a
// constant power-of-2 value:
// (X % pow2C) sgt/slt 0
Expand Down Expand Up @@ -2566,6 +2590,23 @@ Instruction *InstCombinerImpl::foldICmpSRemConstant(ICmpInst &Cmp,
return new ICmpInst(ICmpInst::ICMP_UGT, And, ConstantInt::get(Ty, SignMask));
}

Instruction *InstCombinerImpl::foldICmpURemConstant(ICmpInst &Cmp,
BinaryOperator *URem,
const APInt &C) {
const APInt *C1;
ICmpInst::Predicate Pred = Cmp.getPredicate();
if (match(URem->getOperand(1), m_APInt(C1)) &&
((Pred == ICmpInst::ICMP_ULT && C == *C1 - 1) ||
(Pred == ICmpInst::ICMP_UGT && C == *C1 - 2 && C.ugt(1)))) {
// icmp ult (X u% C), (C - 1) --> icmp ne (X u% C), (C - 1)
// icmp ugt (X u% C), (C - 2) --> icmp eq (X u% C), (C - 1), if C >u 1
return new ICmpInst(Pred == ICmpInst::ICMP_UGT ? ICmpInst::ICMP_EQ
: ICmpInst::ICMP_NE,
URem, ConstantInt::get(URem->getType(), *C1 - 1));
}
return nullptr;
}

/// Fold icmp (udiv X, Y), C.
Instruction *InstCombinerImpl::foldICmpUDivConstant(ICmpInst &Cmp,
BinaryOperator *UDiv,
Expand Down Expand Up @@ -3703,6 +3744,10 @@ Instruction *InstCombinerImpl::foldICmpBinOpWithConstant(ICmpInst &Cmp,
if (Instruction *I = foldICmpSRemConstant(Cmp, BO, C))
return I;
break;
case Instruction::URem:
if (Instruction *I = foldICmpURemConstant(Cmp, BO, C))
return I;
break;
case Instruction::UDiv:
if (Instruction *I = foldICmpUDivConstant(Cmp, BO, C))
return I;
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,8 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
const APInt &C);
Instruction *foldICmpSRemConstant(ICmpInst &Cmp, BinaryOperator *UDiv,
const APInt &C);
Instruction *foldICmpURemConstant(ICmpInst &Cmp, BinaryOperator *UDiv,
const APInt &C);
Instruction *foldICmpUDivConstant(ICmpInst &Cmp, BinaryOperator *UDiv,
const APInt &C);
Instruction *foldICmpDivConstant(ICmpInst &Cmp, BinaryOperator *Div,
Expand Down
Loading