Skip to content

Commit

Permalink
[InstCombine] optimize powi(X,Y) * X with Ofast
Browse files Browse the repository at this point in the history
Try to transform the powi(X, Y) * X into powi(X, Y+1) with Ofast

For this case, when the Y is 3, then powi(X, 4) is replaced by
X2 = X * X; X2 * X2 in the further step.
Similar to D109954, who requires reassoc.

Fixes #69862.
  • Loading branch information
vfdff committed Mar 14, 2024
1 parent 160693d commit 0985202
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
12 changes: 12 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,18 @@ 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 (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 Down
49 changes: 49 additions & 0 deletions llvm/test/Transforms/InstCombine/powi.ll
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,52 @@ define double @fdiv_pow_powi_negative_variable(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 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
}

0 comments on commit 0985202

Please sign in to comment.