Skip to content

Commit 2938f1c

Browse files
committed
[InstCombine] Refactor powi(X,Y) / X to call foldPowiReassoc, NFC
1 parent defc485 commit 2938f1c

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,18 @@ Instruction *InstCombinerImpl::foldPowiReassoc(BinaryOperator &I) {
611611
Y->getType() == Z->getType())
612612
return createPowiExpr(I, *this, X, Y, Z);
613613

614+
// powi(X, Y) / X --> powi(X, Y-1)
615+
// This is legal when (Y - 1) can't wraparound, in which case reassoc and nnan
616+
// are required.
617+
// TODO: Multi-use may be also better off creating Powi(x,y-1)
618+
if (I.hasAllowReassoc() && I.hasNoNaNs() &&
619+
match(Op0, m_OneUse(m_Intrinsic<Intrinsic::powi>(m_Specific(Op1),
620+
m_Value(Y)))) &&
621+
willNotOverflowSignedSub(Y, ConstantInt::get(Y->getType(), 1), I)) {
622+
Constant *NegOne = ConstantInt::getAllOnesValue(Y->getType());
623+
return createPowiExpr(I, *this, Op1, Y, NegOne);
624+
}
625+
614626
return nullptr;
615627
}
616628

@@ -1904,20 +1916,8 @@ Instruction *InstCombinerImpl::visitFDiv(BinaryOperator &I) {
19041916
return replaceInstUsesWith(I, Pow);
19051917
}
19061918

1907-
// powi(X, Y) / X --> powi(X, Y-1)
1908-
// This is legal when (Y - 1) can't wraparound, in which case reassoc and nnan
1909-
// are required.
1910-
// TODO: Multi-use may be also better off creating Powi(x,y-1)
1911-
if (I.hasAllowReassoc() && I.hasNoNaNs() &&
1912-
match(Op0, m_OneUse(m_Intrinsic<Intrinsic::powi>(m_Specific(Op1),
1913-
m_Value(Y)))) &&
1914-
willNotOverflowSignedSub(Y, ConstantInt::get(Y->getType(), 1), I)) {
1915-
Constant *NegOne = ConstantInt::getAllOnesValue(Y->getType());
1916-
Value *Y1 = Builder.CreateAdd(Y, NegOne);
1917-
Type *Types[] = {Op1->getType(), Y1->getType()};
1918-
Value *Pow = Builder.CreateIntrinsic(Intrinsic::powi, Types, {Op1, Y1}, &I);
1919-
return replaceInstUsesWith(I, Pow);
1920-
}
1919+
if (Instruction *FoldedPowi = foldPowiReassoc(I))
1920+
return FoldedPowi;
19211921

19221922
return nullptr;
19231923
}

0 commit comments

Comments
 (0)