Skip to content

[InstCombine] optimize powi(X,Y) * X with Ofast #69998

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

Merged
merged 3 commits into from
Mar 14, 2024
Merged
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
16 changes: 16 additions & 0 deletions llvm/include/llvm/IR/PatternMatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
return SubPattern;
}

template <typename SubPattern_t> struct AllowReassoc_match {
SubPattern_t SubPattern;

AllowReassoc_match(const SubPattern_t &SP) : SubPattern(SP) {}

template <typename OpTy> bool match(OpTy *V) {
auto *I = dyn_cast<FPMathOperator>(V);
return I && I->hasAllowReassoc() && SubPattern.match(I);
}
};

template <typename T>
inline AllowReassoc_match<T> m_AllowReassoc(const T &SubPattern) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't quite the API I had in my mind. I envisioned the required flags as a template parameter to the existing matchers, so you could have something like:
m_FMul<Reassoc>(), m_Intrinsic<powi, Reassoc>

However I wasn't expecting you to do anything for this in this patch. This is fine for now

return SubPattern;
}

template <typename Class> struct class_match {
template <typename ITy> bool match(ITy *V) { return isa<Class>(V); }
};
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
49 changes: 39 additions & 10 deletions llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,42 @@ Instruction *InstCombinerImpl::foldFPSignBitOps(BinaryOperator &I) {
return nullptr;
}

Instruction *InstCombinerImpl::foldPowiReassoc(BinaryOperator &I) {
auto createPowiExpr = [](BinaryOperator &I, InstCombinerImpl &IC, Value *X,
Value *Y, Value *Z) {
InstCombiner::BuilderTy &Builder = IC.Builder;
Value *YZ = Builder.CreateAdd(Y, Z);
auto *NewPow = Builder.CreateIntrinsic(
Intrinsic::powi, {X->getType(), YZ->getType()}, {X, YZ}, &I);
return IC.replaceInstUsesWith(I, NewPow);
};

Value *X, *Y, *Z;

// 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_AllowReassoc(m_Intrinsic<Intrinsic::powi>(
m_Value(X), m_Value(Y)))),
m_Deferred(X)))) {
Constant *One = ConstantInt::get(Y->getType(), 1);
if (willNotOverflowSignedAdd(Y, One, I))
return createPowiExpr(I, *this, X, Y, One);
}

// 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_AllowReassoc(
m_Intrinsic<Intrinsic::powi>(m_Value(X), m_Value(Y)))) &&
match(Op1, m_AllowReassoc(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);
Expand Down Expand Up @@ -683,6 +719,9 @@ Instruction *InstCombinerImpl::foldFMulReassoc(BinaryOperator &I) {
return replaceInstUsesWith(I, Pow);
}

if (Instruction *FoldedPowi = foldPowiReassoc(I))
return FoldedPowi;

if (I.isOnlyUserOfAnyOperand()) {
// pow(X, Y) * pow(X, Z) -> pow(X, Y + Z)
if (match(Op0, m_Intrinsic<Intrinsic::pow>(m_Value(X), m_Value(Y))) &&
Expand All @@ -699,16 +738,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)))) {
Expand Down
137 changes: 117 additions & 20 deletions llvm/test/Transforms/InstCombine/powi.ll
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,55 @@ entry:
ret double %mul
}

define double @powi_fmul_powi_no_reassoc(double %x, i32 %y, i32 %z) {
; CHECK-LABEL: @powi_fmul_powi_no_reassoc(
; Negative test: Missing reassoc flag on fmul
define double @powi_fmul_powi_no_reassoc1(double %x, i32 %y, i32 %z) {
; CHECK-LABEL: @powi_fmul_powi_no_reassoc1(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[P1:%.*]] = tail call double @llvm.powi.f64.i32(double [[X:%.*]], i32 [[Y:%.*]])
; CHECK-NEXT: [[P2:%.*]] = tail call double @llvm.powi.f64.i32(double [[X]], i32 [[Z:%.*]])
; CHECK-NEXT: [[P1:%.*]] = tail call reassoc double @llvm.powi.f64.i32(double [[X:%.*]], i32 [[Y:%.*]])
; CHECK-NEXT: [[P2:%.*]] = tail call reassoc double @llvm.powi.f64.i32(double [[X]], i32 [[Z:%.*]])
; CHECK-NEXT: [[MUL:%.*]] = fmul double [[P2]], [[P1]]
; CHECK-NEXT: ret double [[MUL]]
;
entry:
%p1 = tail call double @llvm.powi.f64.i32(double %x, i32 %y)
%p2 = tail call double @llvm.powi.f64.i32(double %x, i32 %z)
%p1 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %y)
%p2 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %z)
%mul = fmul double %p2, %p1
ret double %mul
}

; Negative test: Missing reassoc flag on 2nd operand
define double @powi_fmul_powi_no_reassoc2(double %x, i32 %y, i32 %z) {
; CHECK-LABEL: @powi_fmul_powi_no_reassoc2(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[P1:%.*]] = tail call reassoc double @llvm.powi.f64.i32(double [[X:%.*]], i32 [[Y:%.*]])
; CHECK-NEXT: [[P2:%.*]] = tail call double @llvm.powi.f64.i32(double [[X]], i32 [[Z:%.*]])
; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[P2]], [[P1]]
; CHECK-NEXT: ret double [[MUL]]
;
entry:
%p1 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %y)
%p2 = tail call double @llvm.powi.f64.i32(double %x, i32 %z)
%mul = fmul reassoc double %p2, %p1
ret double %mul
}

; Negative test: Missing reassoc flag on 1st operand
define double @powi_fmul_powi_no_reassoc3(double %x, i32 %y, i32 %z) {
; CHECK-LABEL: @powi_fmul_powi_no_reassoc3(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[P1:%.*]] = tail call double @llvm.powi.f64.i32(double [[X:%.*]], i32 [[Y:%.*]])
; CHECK-NEXT: [[P2:%.*]] = tail call reassoc double @llvm.powi.f64.i32(double [[X]], i32 [[Z:%.*]])
; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[P2]], [[P1]]
; CHECK-NEXT: ret double [[MUL]]
;
entry:
%p1 = tail call double @llvm.powi.f64.i32(double %x, i32 %y)
%p2 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %z)
%mul = fmul reassoc double %p2, %p1
ret double %mul
}

; All of the fmul and its operands should have the reassoc flags
define double @powi_fmul_powi(double %x, i32 %y, i32 %z) {
; CHECK-LABEL: @powi_fmul_powi(
; CHECK-NEXT: entry:
Expand All @@ -149,8 +182,8 @@ define double @powi_fmul_powi(double %x, i32 %y, i32 %z) {
; CHECK-NEXT: ret double [[MUL]]
;
entry:
%p1 = tail call double @llvm.powi.f64.i32(double %x, i32 %y)
%p2 = tail call double @llvm.powi.f64.i32(double %x, i32 %z)
%p1 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %y)
%p2 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %z)
%mul = fmul reassoc double %p2, %p1
ret double %mul
}
Expand All @@ -163,8 +196,8 @@ define double @powi_fmul_powi_fast_on_fmul(double %x, i32 %y, i32 %z) {
; CHECK-NEXT: ret double [[MUL]]
;
entry:
%p1 = tail call double @llvm.powi.f64.i32(double %x, i32 %y)
%p2 = tail call double @llvm.powi.f64.i32(double %x, i32 %z)
%p1 = tail call fast double @llvm.powi.f64.i32(double %x, i32 %y)
%p2 = tail call fast double @llvm.powi.f64.i32(double %x, i32 %z)
%mul = fmul fast double %p2, %p1
ret double %mul
}
Expand Down Expand Up @@ -192,42 +225,57 @@ define double @powi_fmul_powi_same_power(double %x, i32 %y, i32 %z) {
; CHECK-NEXT: ret double [[MUL]]
;
entry:
%p1 = tail call double @llvm.powi.f64.i32(double %x, i32 %y)
%p2 = tail call double @llvm.powi.f64.i32(double %x, i32 %y)
%p1 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %y)
%p2 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %y)
%mul = fmul reassoc double %p2, %p1
ret double %mul
}

define double @powi_fmul_powi_different_integer_types(double %x, i32 %y, i16 %z) {
; CHECK-LABEL: @powi_fmul_powi_different_integer_types(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[P1:%.*]] = tail call reassoc double @llvm.powi.f64.i32(double [[X:%.*]], i32 [[Y:%.*]])
; CHECK-NEXT: [[P2:%.*]] = tail call reassoc double @llvm.powi.f64.i16(double [[X]], i16 [[Z:%.*]])
; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[P2]], [[P1]]
; CHECK-NEXT: ret double [[MUL]]
;
entry:
%p1 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %y)
%p2 = tail call reassoc double @llvm.powi.f64.i16(double %x, i16 %z)
%mul = fmul reassoc double %p2, %p1
ret double %mul
}

define double @powi_fmul_powi_use_first(double %x, i32 %y, i32 %z) {
; CHECK-LABEL: @powi_fmul_powi_use_first(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[P1:%.*]] = tail call double @llvm.powi.f64.i32(double [[X:%.*]], i32 [[Y:%.*]])
; CHECK-NEXT: [[P1:%.*]] = tail call reassoc double @llvm.powi.f64.i32(double [[X:%.*]], i32 [[Y:%.*]])
; CHECK-NEXT: tail call void @use(double [[P1]])
; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[Y]], [[Z:%.*]]
; CHECK-NEXT: [[MUL:%.*]] = call reassoc double @llvm.powi.f64.i32(double [[X]], i32 [[TMP0]])
; CHECK-NEXT: ret double [[MUL]]
;
entry:
%p1 = tail call double @llvm.powi.f64.i32(double %x, i32 %y)
%p1 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %y)
tail call void @use(double %p1)
%p2 = tail call double @llvm.powi.f64.i32(double %x, i32 %z)
%p2 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %z)
%mul = fmul reassoc double %p1, %p2
ret double %mul
}

define double @powi_fmul_powi_use_second(double %x, i32 %y, i32 %z) {
; CHECK-LABEL: @powi_fmul_powi_use_second(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[P1:%.*]] = tail call double @llvm.powi.f64.i32(double [[X:%.*]], i32 [[Z:%.*]])
; CHECK-NEXT: [[P1:%.*]] = tail call reassoc double @llvm.powi.f64.i32(double [[X:%.*]], i32 [[Z:%.*]])
; CHECK-NEXT: tail call void @use(double [[P1]])
; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[Y:%.*]], [[Z]]
; CHECK-NEXT: [[MUL:%.*]] = call reassoc double @llvm.powi.f64.i32(double [[X]], i32 [[TMP0]])
; CHECK-NEXT: ret double [[MUL]]
;
entry:
%p1 = tail call double @llvm.powi.f64.i32(double %x, i32 %z)
%p1 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %z)
tail call void @use(double %p1)
%p2 = tail call double @llvm.powi.f64.i32(double %x, i32 %y)
%p2 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 %y)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't have a test where the 2 powi declarations have different integer types, should add one

%mul = fmul reassoc double %p2, %p1
ret double %mul
}
Expand Down Expand Up @@ -333,11 +381,60 @@ define double @fdiv_pow_powi_negative(double %x) {
; Negative test: The 2nd powi argument is a variable
define double @fdiv_pow_powi_negative_variable(double %x, i32 %y) {
; CHECK-LABEL: @fdiv_pow_powi_negative_variable(
; CHECK-NEXT: [[P1:%.*]] = call double @llvm.powi.f64.i32(double [[X:%.*]], i32 [[Y:%.*]])
; CHECK-NEXT: [[P1:%.*]] = call reassoc double @llvm.powi.f64.i32(double [[X:%.*]], i32 [[Y:%.*]])
; CHECK-NEXT: [[DIV:%.*]] = fdiv reassoc nnan double [[P1]], [[X]]
; CHECK-NEXT: ret double [[DIV]]
;
%p1 = call double @llvm.powi.f64.i32(double %x, i32 %y)
%p1 = call reassoc double @llvm.powi.f64.i32(double %x, i32 %y)
%div = fdiv reassoc nnan double %p1, %x
ret double %div
}

; powi(X, Y) * X --> powi(X, Y+1)
define double @powi_fmul_powi_x(double noundef %x) {
; CHECK-LABEL: @powi_fmul_powi_x(
; CHECK-NEXT: [[MUL:%.*]] = call reassoc double @llvm.powi.f64.i32(double [[X:%.*]], i32 4)
; CHECK-NEXT: ret double [[MUL]]
;
%p1 = tail call reassoc double @llvm.powi.f64.i32(double %x, i32 3)
%mul = fmul reassoc double %p1, %x
ret double %mul
}

; Negative test: Multi-use
define double @powi_fmul_powi_x_multi_use(double noundef %x) {
; CHECK-LABEL: @powi_fmul_powi_x_multi_use(
; CHECK-NEXT: [[P1:%.*]] = tail call double @llvm.powi.f64.i32(double [[X:%.*]], i32 3)
; CHECK-NEXT: tail call void @use(double [[P1]])
; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[P1]], [[X]]
; CHECK-NEXT: ret double [[MUL]]
;
%p1 = tail call double @llvm.powi.f64.i32(double %x, i32 3)
tail call void @use(double %p1)
%mul = fmul reassoc double %p1, %x
ret double %mul
}

; Negative test: Miss fmf flag
define double @powi_fmul_powi_x_missing_reassoc(double noundef %x) {
; CHECK-LABEL: @powi_fmul_powi_x_missing_reassoc(
; CHECK-NEXT: [[P1:%.*]] = tail call double @llvm.powi.f64.i32(double [[X:%.*]], i32 3)
; CHECK-NEXT: [[MUL:%.*]] = fmul double [[P1]], [[X]]
; CHECK-NEXT: ret double [[MUL]]
;
%p1 = tail call double @llvm.powi.f64.i32(double %x, i32 3)
%mul = fmul double %p1, %x
ret double %mul
}

; Negative test: overflow
define double @powi_fmul_powi_x_overflow(double noundef %x) {
; CHECK-LABEL: @powi_fmul_powi_x_overflow(
; CHECK-NEXT: [[P1:%.*]] = tail call double @llvm.powi.f64.i32(double [[X:%.*]], i32 2147483647)
; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[P1]], [[X]]
; CHECK-NEXT: ret double [[MUL]]
;
%p1 = tail call double @llvm.powi.f64.i32(double %x, i32 2147483647) ; INT_MAX
%mul = fmul reassoc double %p1, %x
ret double %mul
}