Skip to content

[LLVM][SCEV] Look through common multiplicand when simplifying compares. #141798

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions llvm/lib/Analysis/ScalarEvolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10748,6 +10748,32 @@ bool ScalarEvolution::SimplifyICmpOperands(CmpPredicate &Pred, const SCEV *&LHS,
if (Depth >= 3)
return false;

if (isa<SCEVMulExpr>(LHS) && isa<SCEVMulExpr>(RHS)) {
const SCEVMulExpr *LMul = cast<SCEVMulExpr>(LHS);
const SCEVMulExpr *RMul = cast<SCEVMulExpr>(RHS);

if (LMul->getNumOperands() == 2 && RMul->getNumOperands() == 2 &&
LMul->getOperand(1) == RMul->getOperand(1)) {
// (X * Z) uicmp (Y * Z) ==> X uicmp Y
// when neither multiply wraps and Z is non-zero.
if (ICmpInst::isUnsigned(Pred) && isKnownNonZero(LMul->getOperand(1)) &&
LMul->hasNoUnsignedWrap() && RMul->hasNoUnsignedWrap()) {
LHS = LMul->getOperand(0);
RHS = RMul->getOperand(0);
Changed = true;
}
// (X * Z) sicmp (Y * Z) ==> X sicmp Y
// when neither multiply wraps and Z is positive.
else if (ICmpInst::isSigned(Pred) &&
isKnownPositive(LMul->getOperand(1)) &&
LMul->hasNoSignedWrap() && RMul->hasNoSignedWrap()) {
LHS = LMul->getOperand(0);
RHS = RMul->getOperand(0);
Changed = true;
}
}
}

// Canonicalize a constant to the right side.
if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
// Check for both operands constant.
Expand Down
Loading
Loading