Skip to content

Commit 322d458

Browse files
committed
[InstCombine] create a helper function createPowiExpr, NFC
1 parent 0a480d4 commit 322d458

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,22 @@ Instruction *InstCombinerImpl::foldFPSignBitOps(BinaryOperator &I) {
571571
return nullptr;
572572
}
573573

574+
static Instruction *createPowiExpr(BinaryOperator &I, InstCombinerImpl &IC,
575+
Value *X, Value *Y, Value *Z) {
576+
Value *YZ;
577+
InstCombiner::BuilderTy &Builder = IC.Builder;
578+
579+
if (auto *C = dyn_cast<ConstantInt>(Z)) {
580+
if (C->isOne())
581+
YZ = Builder.CreateAdd(Y, ConstantInt::get(Y->getType(), 1));
582+
} else
583+
YZ = Builder.CreateAdd(Y, Z);
584+
585+
auto *NewPow = Builder.CreateIntrinsic(
586+
Intrinsic::powi, {X->getType(), YZ->getType()}, {X, YZ}, &I);
587+
return IC.replaceInstUsesWith(I, NewPow);
588+
}
589+
574590
Instruction *InstCombinerImpl::foldFMulReassoc(BinaryOperator &I) {
575591
Value *Op0 = I.getOperand(0);
576592
Value *Op1 = I.getOperand(1);
@@ -688,12 +704,8 @@ Instruction *InstCombinerImpl::foldFMulReassoc(BinaryOperator &I) {
688704
if (match(&I, m_c_FMul(m_OneUse(m_Intrinsic<Intrinsic::powi>(m_Value(X),
689705
m_Value(Y))),
690706
m_Deferred(X))) &&
691-
willNotOverflowSignedAdd(Y, ConstantInt::get(Y->getType(), 1), I)) {
692-
auto *Y1 = Builder.CreateAdd(Y, ConstantInt::get(Y->getType(), 1));
693-
auto *NewPow = Builder.CreateIntrinsic(
694-
Intrinsic::powi, {X->getType(), Y1->getType()}, {X, Y1}, &I);
695-
return replaceInstUsesWith(I, NewPow);
696-
}
707+
willNotOverflowSignedAdd(Y, ConstantInt::get(Y->getType(), 1), I))
708+
return createPowiExpr(I, *this, X, Y, ConstantInt::get(Y->getType(), 1));
697709

698710
if (I.isOnlyUserOfAnyOperand()) {
699711
// pow(X, Y) * pow(X, Z) -> pow(X, Y + Z)
@@ -714,12 +726,8 @@ Instruction *InstCombinerImpl::foldFMulReassoc(BinaryOperator &I) {
714726
// powi(x, y) * powi(x, z) -> powi(x, y + z)
715727
if (match(Op0, m_Intrinsic<Intrinsic::powi>(m_Value(X), m_Value(Y))) &&
716728
match(Op1, m_Intrinsic<Intrinsic::powi>(m_Specific(X), m_Value(Z))) &&
717-
Y->getType() == Z->getType()) {
718-
auto *YZ = Builder.CreateAdd(Y, Z);
719-
auto *NewPow = Builder.CreateIntrinsic(
720-
Intrinsic::powi, {X->getType(), YZ->getType()}, {X, YZ}, &I);
721-
return replaceInstUsesWith(I, NewPow);
722-
}
729+
Y->getType() == Z->getType())
730+
return createPowiExpr(I, *this, X, Y, Z);
723731

724732
// exp(X) * exp(Y) -> exp(X + Y)
725733
if (match(Op0, m_Intrinsic<Intrinsic::exp>(m_Value(X))) &&

0 commit comments

Comments
 (0)