Skip to content

Commit 8817658

Browse files
committed
[InstCombine] Missed optimization in math expression: simplify calls exp functions
Summary: This patch enables folding following expressions under -ffast-math flag: exp(X) * exp(Y) -> exp(X + Y), exp2(X) * exp2(Y) -> exp2(X + Y). Motivation: https://bugs.llvm.org/show_bug.cgi?id=35594 Reviewers: hfinkel, spatel, efriedma, lebedev.ri Reviewed By: spatel, lebedev.ri Subscribers: lebedev.ri, llvm-commits Differential Revision: https://reviews.llvm.org/D41342 llvm-svn: 352730
1 parent 0e712a7 commit 8817658

File tree

3 files changed

+42
-30
lines changed

3 files changed

+42
-30
lines changed

llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,26 @@ Instruction *InstCombiner::visitFMul(BinaryOperator &I) {
441441
return replaceInstUsesWith(I, Sqrt);
442442
}
443443

444+
// exp(X) * exp(Y) -> exp(X + Y)
445+
// Match as long as at least one of exp has only one use.
446+
if (match(Op0, m_Intrinsic<Intrinsic::exp>(m_Value(X))) &&
447+
match(Op1, m_Intrinsic<Intrinsic::exp>(m_Value(Y))) &&
448+
(Op0->hasOneUse() || Op1->hasOneUse())) {
449+
Value *XY = Builder.CreateFAddFMF(X, Y, &I);
450+
Value *Exp = Builder.CreateUnaryIntrinsic(Intrinsic::exp, XY, &I);
451+
return replaceInstUsesWith(I, Exp);
452+
}
453+
454+
// exp2(X) * exp2(Y) -> exp2(X + Y)
455+
// Match as long as at least one of exp2 has only one use.
456+
if (match(Op0, m_Intrinsic<Intrinsic::exp2>(m_Value(X))) &&
457+
match(Op1, m_Intrinsic<Intrinsic::exp2>(m_Value(Y))) &&
458+
(Op0->hasOneUse() || Op1->hasOneUse())) {
459+
Value *XY = Builder.CreateFAddFMF(X, Y, &I);
460+
Value *Exp2 = Builder.CreateUnaryIntrinsic(Intrinsic::exp2, XY, &I);
461+
return replaceInstUsesWith(I, Exp2);
462+
}
463+
444464
// (X*Y) * X => (X*X) * Y where Y != X
445465
// The purpose is two-fold:
446466
// 1) to form a power expression (of X).

llvm/test/Transforms/InstCombine/fmul-exp.ll

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ define double @exp_a_exp_b(double %a, double %b) {
2121
; exp(a) * exp(b) reassoc, multiple uses
2222
define double @exp_a_exp_b_multiple_uses(double %a, double %b) {
2323
; CHECK-LABEL: @exp_a_exp_b_multiple_uses(
24-
; CHECK-NEXT: [[TMP:%.*]] = call double @llvm.exp.f64(double [[A:%.*]])
2524
; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.exp.f64(double [[B:%.*]])
26-
; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[TMP]], [[TMP1]]
25+
; CHECK-NEXT: [[TMP:%.*]] = fadd reassoc double [[A:%.*]], [[B]]
26+
; CHECK-NEXT: [[TMP2:%.*]] = call reassoc double @llvm.exp.f64(double [[TMP]])
2727
; CHECK-NEXT: call void @use(double [[TMP1]])
28-
; CHECK-NEXT: ret double [[MUL]]
28+
; CHECK-NEXT: ret double [[TMP2]]
2929
;
3030
%tmp = call double @llvm.exp.f64(double %a)
3131
%tmp1 = call double @llvm.exp.f64(double %b)
@@ -55,10 +55,9 @@ define double @exp_a_exp_b_multiple_uses_both(double %a, double %b) {
5555
; exp(a) * exp(b) => exp(a+b) with reassoc
5656
define double @exp_a_exp_b_reassoc(double %a, double %b) {
5757
; CHECK-LABEL: @exp_a_exp_b_reassoc(
58-
; CHECK-NEXT: [[TMP:%.*]] = call double @llvm.exp.f64(double [[A:%.*]])
59-
; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.exp.f64(double [[B:%.*]])
60-
; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[TMP]], [[TMP1]]
61-
; CHECK-NEXT: ret double [[MUL]]
58+
; CHECK-NEXT: [[TMP:%.*]] = fadd reassoc double [[A:%.*]], [[B:%.*]]
59+
; CHECK-NEXT: [[TMP1:%.*]] = call reassoc double @llvm.exp.f64(double [[TMP]])
60+
; CHECK-NEXT: ret double [[TMP1]]
6261
;
6362
%tmp = call double @llvm.exp.f64(double %a)
6463
%tmp1 = call double @llvm.exp.f64(double %b)
@@ -69,14 +68,11 @@ define double @exp_a_exp_b_reassoc(double %a, double %b) {
6968
; exp(a) * exp(b) * exp(c) * exp(d) => exp(a+b+c+d) with reassoc
7069
define double @exp_a_exp_b_exp_c_exp_d_fast(double %a, double %b, double %c, double %d) {
7170
; CHECK-LABEL: @exp_a_exp_b_exp_c_exp_d_fast(
72-
; CHECK-NEXT: [[TMP:%.*]] = call double @llvm.exp.f64(double [[A:%.*]])
73-
; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.exp.f64(double [[B:%.*]])
74-
; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[TMP]], [[TMP1]]
75-
; CHECK-NEXT: [[TMP2:%.*]] = call double @llvm.exp.f64(double [[C:%.*]])
76-
; CHECK-NEXT: [[MUL1:%.*]] = fmul reassoc double [[MUL]], [[TMP2]]
77-
; CHECK-NEXT: [[TMP3:%.*]] = call double @llvm.exp.f64(double [[D:%.*]])
78-
; CHECK-NEXT: [[MUL2:%.*]] = fmul reassoc double [[MUL1]], [[TMP3]]
79-
; CHECK-NEXT: ret double [[MUL2]]
71+
; CHECK-NEXT: [[TMP:%.*]] = fadd reassoc double [[A:%.*]], [[B:%.*]]
72+
; CHECK-NEXT: [[TMP1:%.*]] = fadd reassoc double [[TMP]], [[C:%.*]]
73+
; CHECK-NEXT: [[TMP2:%.*]] = fadd reassoc double [[TMP1]], [[D:%.*]]
74+
; CHECK-NEXT: [[TMP3:%.*]] = call reassoc double @llvm.exp.f64(double [[TMP2]])
75+
; CHECK-NEXT: ret double [[TMP3]]
8076
;
8177
%tmp = call double @llvm.exp.f64(double %a)
8278
%tmp1 = call double @llvm.exp.f64(double %b)

llvm/test/Transforms/InstCombine/fmul-exp2.ll

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ define double @exp2_a_exp2_b(double %a, double %b) {
2121
; exp2(a) * exp2(b) reassoc, multiple uses
2222
define double @exp2_a_exp2_b_multiple_uses(double %a, double %b) {
2323
; CHECK-LABEL: @exp2_a_exp2_b_multiple_uses(
24-
; CHECK-NEXT: [[TMP:%.*]] = call double @llvm.exp2.f64(double [[A:%.*]])
2524
; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.exp2.f64(double [[B:%.*]])
26-
; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[TMP]], [[TMP1]]
25+
; CHECK-NEXT: [[TMP:%.*]] = fadd reassoc double [[A:%.*]], [[B]]
26+
; CHECK-NEXT: [[TMP2:%.*]] = call reassoc double @llvm.exp2.f64(double [[TMP]])
2727
; CHECK-NEXT: call void @use(double [[TMP1]])
28-
; CHECK-NEXT: ret double [[MUL]]
28+
; CHECK-NEXT: ret double [[TMP2]]
2929
;
3030
%tmp = call double @llvm.exp2.f64(double %a)
3131
%tmp1 = call double @llvm.exp2.f64(double %b)
@@ -55,10 +55,9 @@ define double @exp2_a_exp2_b_multiple_uses_both(double %a, double %b) {
5555
; exp2(a) * exp2(b) => exp2(a+b) with reassoc
5656
define double @exp2_a_exp2_b_reassoc(double %a, double %b) {
5757
; CHECK-LABEL: @exp2_a_exp2_b_reassoc(
58-
; CHECK-NEXT: [[TMP:%.*]] = call double @llvm.exp2.f64(double [[A:%.*]])
59-
; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.exp2.f64(double [[B:%.*]])
60-
; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[TMP]], [[TMP1]]
61-
; CHECK-NEXT: ret double [[MUL]]
58+
; CHECK-NEXT: [[TMP:%.*]] = fadd reassoc double [[A:%.*]], [[B:%.*]]
59+
; CHECK-NEXT: [[TMP1:%.*]] = call reassoc double @llvm.exp2.f64(double [[TMP]])
60+
; CHECK-NEXT: ret double [[TMP1]]
6261
;
6362
%tmp = call double @llvm.exp2.f64(double %a)
6463
%tmp1 = call double @llvm.exp2.f64(double %b)
@@ -69,14 +68,11 @@ define double @exp2_a_exp2_b_reassoc(double %a, double %b) {
6968
; exp2(a) * exp2(b) * exp2(c) * exp2(d) => exp2(a+b+c+d) with reassoc
7069
define double @exp2_a_exp2_b_exp2_c_exp2_d(double %a, double %b, double %c, double %d) {
7170
; CHECK-LABEL: @exp2_a_exp2_b_exp2_c_exp2_d(
72-
; CHECK-NEXT: [[TMP:%.*]] = call double @llvm.exp2.f64(double [[A:%.*]])
73-
; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.exp2.f64(double [[B:%.*]])
74-
; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[TMP]], [[TMP1]]
75-
; CHECK-NEXT: [[TMP2:%.*]] = call double @llvm.exp2.f64(double [[C:%.*]])
76-
; CHECK-NEXT: [[MUL1:%.*]] = fmul reassoc double [[MUL]], [[TMP2]]
77-
; CHECK-NEXT: [[TMP3:%.*]] = call double @llvm.exp2.f64(double [[D:%.*]])
78-
; CHECK-NEXT: [[MUL2:%.*]] = fmul reassoc double [[MUL1]], [[TMP3]]
79-
; CHECK-NEXT: ret double [[MUL2]]
71+
; CHECK-NEXT: [[TMP:%.*]] = fadd reassoc double [[A:%.*]], [[B:%.*]]
72+
; CHECK-NEXT: [[TMP1:%.*]] = fadd reassoc double [[TMP]], [[C:%.*]]
73+
; CHECK-NEXT: [[TMP2:%.*]] = fadd reassoc double [[TMP1]], [[D:%.*]]
74+
; CHECK-NEXT: [[TMP3:%.*]] = call reassoc double @llvm.exp2.f64(double [[TMP2]])
75+
; CHECK-NEXT: ret double [[TMP3]]
8076
;
8177
%tmp = call double @llvm.exp2.f64(double %a)
8278
%tmp1 = call double @llvm.exp2.f64(double %b)

0 commit comments

Comments
 (0)