]> granicus.if.org Git - llvm/commitdiff
[InstCombine] Missed optimization in math expression: simplify calls exp functions
authorDmitry Venikov <quolyk@gmail.com>
Thu, 31 Jan 2019 06:28:10 +0000 (06:28 +0000)
committerDmitry Venikov <quolyk@gmail.com>
Thu, 31 Jan 2019 06:28:10 +0000 (06:28 +0000)
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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@352730 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
test/Transforms/InstCombine/fmul-exp.ll
test/Transforms/InstCombine/fmul-exp2.ll

index ca67348d05c17e067bb71f1174be448b1dad9f24..ae3f081f50889b162c7648cf81282cf7d2820a63 100644 (file)
@@ -441,6 +441,26 @@ Instruction *InstCombiner::visitFMul(BinaryOperator &I) {
       return replaceInstUsesWith(I, Sqrt);
     }
 
+    // exp(X) * exp(Y) -> exp(X + Y)
+    // Match as long as at least one of exp has only one use.
+    if (match(Op0, m_Intrinsic<Intrinsic::exp>(m_Value(X))) &&
+        match(Op1, m_Intrinsic<Intrinsic::exp>(m_Value(Y))) &&
+        (Op0->hasOneUse() || Op1->hasOneUse())) {
+      Value *XY = Builder.CreateFAddFMF(X, Y, &I);
+      Value *Exp = Builder.CreateUnaryIntrinsic(Intrinsic::exp, XY, &I);
+      return replaceInstUsesWith(I, Exp);
+    }
+
+    // exp2(X) * exp2(Y) -> exp2(X + Y)
+    // Match as long as at least one of exp2 has only one use.
+    if (match(Op0, m_Intrinsic<Intrinsic::exp2>(m_Value(X))) &&
+        match(Op1, m_Intrinsic<Intrinsic::exp2>(m_Value(Y))) &&
+        (Op0->hasOneUse() || Op1->hasOneUse())) {
+      Value *XY = Builder.CreateFAddFMF(X, Y, &I);
+      Value *Exp2 = Builder.CreateUnaryIntrinsic(Intrinsic::exp2, XY, &I);
+      return replaceInstUsesWith(I, Exp2);
+    }
+
     // (X*Y) * X => (X*X) * Y where Y != X
     //  The purpose is two-fold:
     //   1) to form a power expression (of X).
index 50f9245479bdcd44b00eb21ee856f02ef9c19517..28542f45294896bd8cb5a314c9d35f65b5cfa1d5 100644 (file)
@@ -21,11 +21,11 @@ define double @exp_a_exp_b(double %a, double %b) {
 ; exp(a) * exp(b) reassoc, multiple uses
 define double @exp_a_exp_b_multiple_uses(double %a, double %b) {
 ; CHECK-LABEL: @exp_a_exp_b_multiple_uses(
-; CHECK-NEXT:    [[TMP:%.*]] = call double @llvm.exp.f64(double [[A:%.*]])
 ; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.exp.f64(double [[B:%.*]])
-; CHECK-NEXT:    [[MUL:%.*]] = fmul reassoc double [[TMP]], [[TMP1]]
+; CHECK-NEXT:    [[TMP:%.*]] = fadd reassoc double [[A:%.*]], [[B]]
+; CHECK-NEXT:    [[TMP2:%.*]] = call reassoc double @llvm.exp.f64(double [[TMP]])
 ; CHECK-NEXT:    call void @use(double [[TMP1]])
-; CHECK-NEXT:    ret double [[MUL]]
+; CHECK-NEXT:    ret double [[TMP2]]
 ;
   %tmp = call double @llvm.exp.f64(double %a)
   %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) {
 ; exp(a) * exp(b) => exp(a+b) with reassoc
 define double @exp_a_exp_b_reassoc(double %a, double %b) {
 ; CHECK-LABEL: @exp_a_exp_b_reassoc(
-; CHECK-NEXT:    [[TMP:%.*]] = call double @llvm.exp.f64(double [[A:%.*]])
-; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.exp.f64(double [[B:%.*]])
-; CHECK-NEXT:    [[MUL:%.*]] = fmul reassoc double [[TMP]], [[TMP1]]
-; CHECK-NEXT:    ret double [[MUL]]
+; CHECK-NEXT:    [[TMP:%.*]] = fadd reassoc double [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT:    [[TMP1:%.*]] = call reassoc double @llvm.exp.f64(double [[TMP]])
+; CHECK-NEXT:    ret double [[TMP1]]
 ;
   %tmp = call double @llvm.exp.f64(double %a)
   %tmp1 = call double @llvm.exp.f64(double %b)
@@ -69,14 +68,11 @@ define double @exp_a_exp_b_reassoc(double %a, double %b) {
 ; exp(a) * exp(b) * exp(c) * exp(d) => exp(a+b+c+d) with reassoc
 define double @exp_a_exp_b_exp_c_exp_d_fast(double %a, double %b, double %c, double %d) {
 ; CHECK-LABEL: @exp_a_exp_b_exp_c_exp_d_fast(
-; CHECK-NEXT:    [[TMP:%.*]] = call double @llvm.exp.f64(double [[A:%.*]])
-; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.exp.f64(double [[B:%.*]])
-; CHECK-NEXT:    [[MUL:%.*]] = fmul reassoc double [[TMP]], [[TMP1]]
-; CHECK-NEXT:    [[TMP2:%.*]] = call double @llvm.exp.f64(double [[C:%.*]])
-; CHECK-NEXT:    [[MUL1:%.*]] = fmul reassoc double [[MUL]], [[TMP2]]
-; CHECK-NEXT:    [[TMP3:%.*]] = call double @llvm.exp.f64(double [[D:%.*]])
-; CHECK-NEXT:    [[MUL2:%.*]] = fmul reassoc double [[MUL1]], [[TMP3]]
-; CHECK-NEXT:    ret double [[MUL2]]
+; CHECK-NEXT:    [[TMP:%.*]] = fadd reassoc double [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT:    [[TMP1:%.*]] = fadd reassoc double [[TMP]], [[C:%.*]]
+; CHECK-NEXT:    [[TMP2:%.*]] = fadd reassoc double [[TMP1]], [[D:%.*]]
+; CHECK-NEXT:    [[TMP3:%.*]] = call reassoc double @llvm.exp.f64(double [[TMP2]])
+; CHECK-NEXT:    ret double [[TMP3]]
 ;
   %tmp = call double @llvm.exp.f64(double %a)
   %tmp1 = call double @llvm.exp.f64(double %b)
index d65c32e7d0cc4917bd20e39436aa36d597d24e84..f09013847bb6fc960cfe00a7da1b5fcb89184754 100644 (file)
@@ -21,11 +21,11 @@ define double @exp2_a_exp2_b(double %a, double %b) {
 ; exp2(a) * exp2(b) reassoc, multiple uses
 define double @exp2_a_exp2_b_multiple_uses(double %a, double %b) {
 ; CHECK-LABEL: @exp2_a_exp2_b_multiple_uses(
-; CHECK-NEXT:    [[TMP:%.*]] = call double @llvm.exp2.f64(double [[A:%.*]])
 ; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.exp2.f64(double [[B:%.*]])
-; CHECK-NEXT:    [[MUL:%.*]] = fmul reassoc double [[TMP]], [[TMP1]]
+; CHECK-NEXT:    [[TMP:%.*]] = fadd reassoc double [[A:%.*]], [[B]]
+; CHECK-NEXT:    [[TMP2:%.*]] = call reassoc double @llvm.exp2.f64(double [[TMP]])
 ; CHECK-NEXT:    call void @use(double [[TMP1]])
-; CHECK-NEXT:    ret double [[MUL]]
+; CHECK-NEXT:    ret double [[TMP2]]
 ;
   %tmp = call double @llvm.exp2.f64(double %a)
   %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) {
 ; exp2(a) * exp2(b) => exp2(a+b) with reassoc
 define double @exp2_a_exp2_b_reassoc(double %a, double %b) {
 ; CHECK-LABEL: @exp2_a_exp2_b_reassoc(
-; CHECK-NEXT:    [[TMP:%.*]] = call double @llvm.exp2.f64(double [[A:%.*]])
-; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.exp2.f64(double [[B:%.*]])
-; CHECK-NEXT:    [[MUL:%.*]] = fmul reassoc double [[TMP]], [[TMP1]]
-; CHECK-NEXT:    ret double [[MUL]]
+; CHECK-NEXT:    [[TMP:%.*]] = fadd reassoc double [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT:    [[TMP1:%.*]] = call reassoc double @llvm.exp2.f64(double [[TMP]])
+; CHECK-NEXT:    ret double [[TMP1]]
 ;
   %tmp = call double @llvm.exp2.f64(double %a)
   %tmp1 = call double @llvm.exp2.f64(double %b)
@@ -69,14 +68,11 @@ define double @exp2_a_exp2_b_reassoc(double %a, double %b) {
 ; exp2(a) * exp2(b) * exp2(c) * exp2(d) => exp2(a+b+c+d) with reassoc
 define double @exp2_a_exp2_b_exp2_c_exp2_d(double %a, double %b, double %c, double %d) {
 ; CHECK-LABEL: @exp2_a_exp2_b_exp2_c_exp2_d(
-; CHECK-NEXT:    [[TMP:%.*]] = call double @llvm.exp2.f64(double [[A:%.*]])
-; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.exp2.f64(double [[B:%.*]])
-; CHECK-NEXT:    [[MUL:%.*]] = fmul reassoc double [[TMP]], [[TMP1]]
-; CHECK-NEXT:    [[TMP2:%.*]] = call double @llvm.exp2.f64(double [[C:%.*]])
-; CHECK-NEXT:    [[MUL1:%.*]] = fmul reassoc double [[MUL]], [[TMP2]]
-; CHECK-NEXT:    [[TMP3:%.*]] = call double @llvm.exp2.f64(double [[D:%.*]])
-; CHECK-NEXT:    [[MUL2:%.*]] = fmul reassoc double [[MUL1]], [[TMP3]]
-; CHECK-NEXT:    ret double [[MUL2]]
+; CHECK-NEXT:    [[TMP:%.*]] = fadd reassoc double [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT:    [[TMP1:%.*]] = fadd reassoc double [[TMP]], [[C:%.*]]
+; CHECK-NEXT:    [[TMP2:%.*]] = fadd reassoc double [[TMP1]], [[D:%.*]]
+; CHECK-NEXT:    [[TMP3:%.*]] = call reassoc double @llvm.exp2.f64(double [[TMP2]])
+; CHECK-NEXT:    ret double [[TMP3]]
 ;
   %tmp = call double @llvm.exp2.f64(double %a)
   %tmp1 = call double @llvm.exp2.f64(double %b)