[llvm] 1752b9e - [InstCombine] create a helper function foldPowiReassoc, NFC
via llvm-commits
llvm-commits at lists.llvm.org
Thu Mar 14 07:06:44 PDT 2024
Author: zhongyunde 00443407
Date: 2024-03-14T22:05:20+08:00
New Revision: 1752b9e4c74bda8bb312dadec0dae007c877e28c
URL: https://github.com/llvm/llvm-project/commit/1752b9e4c74bda8bb312dadec0dae007c877e28c
DIFF: https://github.com/llvm/llvm-project/commit/1752b9e4c74bda8bb312dadec0dae007c877e28c.diff
LOG: [InstCombine] create a helper function foldPowiReassoc, NFC
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineInternal.h
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
index 6a1ef6edeb4077..e2b744ba66f2a9 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
+++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
@@ -98,6 +98,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
Instruction *visitSub(BinaryOperator &I);
Instruction *visitFSub(BinaryOperator &I);
Instruction *visitMul(BinaryOperator &I);
+ Instruction *foldPowiReassoc(BinaryOperator &I);
Instruction *foldFMulReassoc(BinaryOperator &I);
Instruction *visitFMul(BinaryOperator &I);
Instruction *visitURem(BinaryOperator &I);
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index 55d89c6108f033..c9df10a9e08fa2 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -571,6 +571,44 @@ Instruction *InstCombinerImpl::foldFPSignBitOps(BinaryOperator &I) {
return nullptr;
}
+Instruction *InstCombinerImpl::foldPowiReassoc(BinaryOperator &I) {
+ Value *X, *Y, *Z;
+ auto createPowiExpr = [](BinaryOperator &I, InstCombinerImpl &IC, Value *X,
+ Value *Y, Value *Z) {
+ Value *YZ;
+ InstCombiner::BuilderTy &Builder = IC.Builder;
+
+ if (auto *C = dyn_cast<ConstantInt>(Z)) {
+ if (C->isOne())
+ YZ = Builder.CreateAdd(Y, ConstantInt::get(Y->getType(), 1));
+ } else
+ YZ = Builder.CreateAdd(Y, Z);
+
+ auto *NewPow = Builder.CreateIntrinsic(
+ Intrinsic::powi, {X->getType(), YZ->getType()}, {X, YZ}, &I);
+ return IC.replaceInstUsesWith(I, NewPow);
+ };
+
+ // powi(X, Y) * X --> powi(X, Y+1)
+ // X * powi(X, Y) --> powi(X, Y+1)
+ if (match(&I, m_c_FMul(m_OneUse(m_Intrinsic<Intrinsic::powi>(m_Value(X),
+ m_Value(Y))),
+ m_Deferred(X))) &&
+ willNotOverflowSignedAdd(Y, ConstantInt::get(Y->getType(), 1), I))
+ return createPowiExpr(I, *this, X, Y, ConstantInt::get(Y->getType(), 1));
+
+ // powi(x, y) * powi(x, z) -> powi(x, y + z)
+ Value *Op0 = I.getOperand(0);
+ Value *Op1 = I.getOperand(1);
+ if (I.isOnlyUserOfAnyOperand() &&
+ match(Op0, m_Intrinsic<Intrinsic::powi>(m_Value(X), m_Value(Y))) &&
+ match(Op1, m_Intrinsic<Intrinsic::powi>(m_Specific(X), m_Value(Z))) &&
+ Y->getType() == Z->getType())
+ return createPowiExpr(I, *this, X, Y, Z);
+
+ return nullptr;
+}
+
Instruction *InstCombinerImpl::foldFMulReassoc(BinaryOperator &I) {
Value *Op0 = I.getOperand(0);
Value *Op1 = I.getOperand(1);
@@ -683,17 +721,8 @@ Instruction *InstCombinerImpl::foldFMulReassoc(BinaryOperator &I) {
return replaceInstUsesWith(I, Pow);
}
- // powi(X, Y) * X --> powi(X, Y+1)
- // X * powi(X, Y) --> powi(X, Y+1)
- if (match(&I, m_c_FMul(m_OneUse(m_Intrinsic<Intrinsic::powi>(m_Value(X),
- m_Value(Y))),
- m_Deferred(X))) &&
- willNotOverflowSignedAdd(Y, ConstantInt::get(Y->getType(), 1), I)) {
- auto *Y1 = Builder.CreateAdd(Y, ConstantInt::get(Y->getType(), 1));
- auto *NewPow = Builder.CreateIntrinsic(
- Intrinsic::powi, {X->getType(), Y1->getType()}, {X, Y1}, &I);
- return replaceInstUsesWith(I, NewPow);
- }
+ if (Instruction *FoldedPowi = foldPowiReassoc(I))
+ return FoldedPowi;
if (I.isOnlyUserOfAnyOperand()) {
// pow(X, Y) * pow(X, Z) -> pow(X, Y + Z)
@@ -711,16 +740,6 @@ Instruction *InstCombinerImpl::foldFMulReassoc(BinaryOperator &I) {
return replaceInstUsesWith(I, NewPow);
}
- // powi(x, y) * powi(x, z) -> powi(x, y + z)
- if (match(Op0, m_Intrinsic<Intrinsic::powi>(m_Value(X), m_Value(Y))) &&
- match(Op1, m_Intrinsic<Intrinsic::powi>(m_Specific(X), m_Value(Z))) &&
- Y->getType() == Z->getType()) {
- auto *YZ = Builder.CreateAdd(Y, Z);
- auto *NewPow = Builder.CreateIntrinsic(
- Intrinsic::powi, {X->getType(), YZ->getType()}, {X, YZ}, &I);
- return replaceInstUsesWith(I, NewPow);
- }
-
// exp(X) * exp(Y) -> exp(X + Y)
if (match(Op0, m_Intrinsic<Intrinsic::exp>(m_Value(X))) &&
match(Op1, m_Intrinsic<Intrinsic::exp>(m_Value(Y)))) {
More information about the llvm-commits
mailing list