Skip to content

[InstCombine] Optimize sub(sext(add(x,y)),sext(add(x,z))). #144174

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 7 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
8 changes: 8 additions & 0 deletions llvm/include/llvm/IR/PatternMatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,14 @@ m_NSWAdd(const LHS &L, const RHS &R) {
R);
}
template <typename LHS, typename RHS>
inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
OverflowingBinaryOperator::NoSignedWrap, true>
m_c_NSWAdd(const LHS &L, const RHS &R) {
return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
OverflowingBinaryOperator::NoSignedWrap,
true>(L, R);
}
template <typename LHS, typename RHS>
inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
OverflowingBinaryOperator::NoSignedWrap>
m_NSWSub(const LHS &L, const RHS &R) {
Expand Down
51 changes: 48 additions & 3 deletions llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1868,7 +1868,7 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
{Sub, Builder.getFalse()});
Value *Ret = Builder.CreateSub(
ConstantInt::get(A->getType(), A->getType()->getScalarSizeInBits()),
Ctlz, "", /*HasNUW*/ true, /*HasNSW*/ true);
Ctlz, "", /*HasNUW=*/true, /*HasNSW=*/true);
return replaceInstUsesWith(I, Builder.CreateZExtOrTrunc(Ret, I.getType()));
}

Expand Down Expand Up @@ -2335,8 +2335,8 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
OverflowingBinaryOperator *LHSSub = cast<OverflowingBinaryOperator>(Op0);
bool HasNUW = I.hasNoUnsignedWrap() && LHSSub->hasNoUnsignedWrap();
bool HasNSW = HasNUW && I.hasNoSignedWrap() && LHSSub->hasNoSignedWrap();
Value *Add = Builder.CreateAdd(Y, Op1, "", /* HasNUW */ HasNUW,
/* HasNSW */ HasNSW);
Value *Add = Builder.CreateAdd(Y, Op1, "", /*HasNUW=*/HasNUW,
/*HasNSW=*/HasNSW);
BinaryOperator *Sub = BinaryOperator::CreateSub(X, Add);
Sub->setHasNoUnsignedWrap(HasNUW);
Sub->setHasNoSignedWrap(HasNSW);
Expand Down Expand Up @@ -2807,6 +2807,51 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
if (Instruction *Res = foldBinOpOfSelectAndCastOfSelectCondition(I))
return Res;

// (sub (sext (add nsw (X, Y)), sext (X))) --> (sext (Y))
if (match(Op1, m_SExtLike(m_Value(X))) &&
match(Op0, m_SExtLike(m_c_NSWAdd(m_Specific(X), m_Value(Y))))) {
Value *SExtY = Builder.CreateSExt(Y, I.getType());
return replaceInstUsesWith(I, SExtY);
}

// (sub[ nsw] (sext (add nsw (X, Y)), sext (add nsw (X, Z)))) -->
// --> (sub[ nsw] (sext (Y), sext (Z)))
{
Value *Z, *Add0, *Add1;
if (match(Op0, m_SExtLike(m_Value(Add0))) &&
match(Op1, m_SExtLike(m_Value(Add1))) &&
((match(Add0, m_NSWAdd(m_Value(X), m_Value(Y))) &&
match(Add1, m_c_NSWAdd(m_Specific(X), m_Value(Z)))) ||
(match(Add0, m_NSWAdd(m_Value(Y), m_Value(X))) &&
match(Add1, m_c_NSWAdd(m_Specific(X), m_Value(Z)))))) {
unsigned NumOfNewInstrs = 0;
// Non-constant Y, Z require new SExt.
NumOfNewInstrs += !isa<Constant>(Y) ? 1 : 0;
NumOfNewInstrs += !isa<Constant>(Z) ? 1 : 0;
// Check if we can trade some of the old instructions for the new ones.
unsigned NumOfDeadInstrs = 0;
if (Op0->hasOneUse()) {
// If Op0 (sext) has multiple uses, then we keep it
// and the add that it uses, otherwise, we can remove
// the sext and probably the add (depending on the number of its uses).
++NumOfDeadInstrs;
NumOfDeadInstrs += Add0->hasOneUse() ? 1 : 0;
}
if (Op1->hasOneUse()) {
++NumOfDeadInstrs;
NumOfDeadInstrs += Add1->hasOneUse() ? 1 : 0;
}
if (NumOfDeadInstrs >= NumOfNewInstrs) {
Value *SExtY = Builder.CreateSExt(Y, I.getType());
Value *SExtZ = Builder.CreateSExt(Z, I.getType());
Value *Sub = Builder.CreateSub(SExtY, SExtZ, "",
/*HasNUW=*/false,
/*HasNSW=*/I.hasNoSignedWrap());
return replaceInstUsesWith(I, Sub);
}
}
}

return TryToNarrowDeduceFlags();
}

Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
auto *Op1C = cast<Constant>(Op1);
return replaceInstUsesWith(
I, Builder.CreateMul(NegOp0, ConstantExpr::getNeg(Op1C), "",
/* HasNUW */ false,
/*HasNUW=*/false,
HasNSW && Op1C->isNotMinSignedValue()));
}

Expand Down Expand Up @@ -1243,8 +1243,8 @@ static Value *foldIDivShl(BinaryOperator &I, InstCombiner::BuilderTy &Builder) {
// or divisor has nsw and operator is sdiv.
Value *Dividend = Builder.CreateShl(
One, Y, "shl.dividend",
/*HasNUW*/ true,
/*HasNSW*/
/*HasNUW=*/true,
/*HasNSW=*/
IsSigned ? (Shl0->hasNoUnsignedWrap() || Shl1->hasNoUnsignedWrap())
: Shl0->hasNoSignedWrap());
return Builder.CreateLShr(Dividend, Z, "", I.isExact());
Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ std::array<Value *, 2> Negator::getSortedOperandsOfBinOp(Instruction *I) {
// However, only do this either if the old `sub` doesn't stick around, or
// it was subtracting from a constant. Otherwise, this isn't profitable.
return Builder.CreateSub(I->getOperand(1), I->getOperand(0),
I->getName() + ".neg", /* HasNUW */ false,
I->getName() + ".neg", /*HasNUW=*/false,
IsNSW && I->hasNoSignedWrap());
}

Expand Down Expand Up @@ -404,15 +404,15 @@ std::array<Value *, 2> Negator::getSortedOperandsOfBinOp(Instruction *I) {
IsNSW &= I->hasNoSignedWrap();
if (Value *NegOp0 = negate(I->getOperand(0), IsNSW, Depth + 1))
return Builder.CreateShl(NegOp0, I->getOperand(1), I->getName() + ".neg",
/* HasNUW */ false, IsNSW);
/*HasNUW=*/false, IsNSW);
// Otherwise, `shl %x, C` can be interpreted as `mul %x, 1<<C`.
Constant *Op1C;
if (!match(I->getOperand(1), m_ImmConstant(Op1C)) || !IsTrulyNegation)
return nullptr;
return Builder.CreateMul(
I->getOperand(0),
Builder.CreateShl(Constant::getAllOnesValue(Op1C->getType()), Op1C),
I->getName() + ".neg", /* HasNUW */ false, IsNSW);
I->getName() + ".neg", /*HasNUW=*/false, IsNSW);
}
case Instruction::Or: {
if (!cast<PossiblyDisjointInst>(I)->isDisjoint())
Expand Down Expand Up @@ -483,7 +483,7 @@ std::array<Value *, 2> Negator::getSortedOperandsOfBinOp(Instruction *I) {
// Can't negate either of them.
return nullptr;
return Builder.CreateMul(NegatedOp, OtherOp, I->getName() + ".neg",
/* HasNUW */ false, IsNSW && I->hasNoSignedWrap());
/*HasNUW=*/false, IsNSW && I->hasNoSignedWrap());
}
default:
return nullptr; // Don't know, likely not negatible for free.
Expand Down
Loading