[llvm] Implement foldICmpRemConstant in InstCombineCompares (PR #77410)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 8 23:24:20 PST 2024
================
@@ -2572,6 +2572,46 @@ Instruction *InstCombinerImpl::foldICmpSRemConstant(ICmpInst &Cmp,
return new ICmpInst(ICmpInst::ICMP_UGT, And, ConstantInt::get(Ty, SignMask));
}
+Instruction *InstCombinerImpl::foldICmpRemConstant(ICmpInst &Cmp,
+ BinaryOperator *Rem,
+ const APInt &C) {
+ const ICmpInst::Predicate Pred = Cmp.getPredicate();
+ Value *X = Rem->getOperand(0);
+ Value *Y = Rem->getOperand(1);
+
+ // Check if the remainder operation is in the required form.
+ if (!isa<BinaryOperator>(X) || !isa<BinaryOperator>(Y))
+ return nullptr;
+
+ BinaryOperator *MulX = cast<BinaryOperator>(X);
+ BinaryOperator *MulY = cast<BinaryOperator>(Y);
+
+ // Check if the operands are multiplication operations.
+ if (MulX->getOpcode() != Instruction::Mul || MulY->getOpcode() != Instruction::Mul)
+ return nullptr;
+
+ // Get the multiplication operands and constants.
+ Value *A = MulX->getOperand(0);
+ Value *C1 = MulX->getOperand(1);
+ Value *B = MulY->getOperand(0);
+ Value *C2 = MulY->getOperand(1);
+
+ const APInt *C1Value, *C2Value;
+
+ // Check if the constants satisfy the condition c1 % c2 == 0.
+ if (!match(C1, m_APInt(C1Value)) || !match(C2, m_APInt(C2Value)) || C1Value->urem(*C2Value) != 0)
+ return nullptr;
----------------
dtcxzyw wrote:
```suggestion
Value *A, *B;
const APInt *C1, *C2;
if (Rem->getOpcode() == Instruction::SRem) {
if (!match(X, m_NSWMul(m_Value(A), m_APInt(C1))))
retur nullptr;
if (!match(Y, m_NSWMul(m_Value(B), m_APInt(C2))))
retur nullptr;
if (!C1->srem(*C2).isZero())
return nullptr;
}
else {
... // match with m_NUWMul and use urem
}
```
Be careful of `nsw/nuw` flags.
https://github.com/llvm/llvm-project/pull/77410
More information about the llvm-commits
mailing list