# [PATCH] D41342: [WIP][InstCombine] Missed optimization in math expression: simplify calls exp functions

Dmitry Venikov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 18 01:21:44 PST 2017

Quolyk created this revision.
Quolyk added reviewers: hfinkel, davide, spatel.

Motivation: https://bugs.llvm.org/show_bug.cgi?id=35594. Related https://reviews.llvm.org/D41322, https://reviews.llvm.org/D41286, https://reviews.llvm.org/D41283

https://reviews.llvm.org/D41342

Files:
lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
test/Transforms/InstCombine/fmul.ll

Index: test/Transforms/InstCombine/fmul.ll
===================================================================
--- test/Transforms/InstCombine/fmul.ll
+++ test/Transforms/InstCombine/fmul.ll
@@ -181,3 +181,31 @@
%mul = fmul float %x.fabs, %y.fabs
ret float %mul
}
+
+; CHECK-LABEL @exp_a_exp_b(
+; CHECK: fadd fast double %a, %b
+; CHECK: call fast double @llvm.exp.f64(double %1)
+define double @exp_a_exp_b(double %a, double %b) {
+  %1 = call fast double @llvm.exp.f64(double %a)
+  %2 = call fast double @llvm.exp.f64(double %b)
+  %mul = fmul fast double %1, %2
+  ret double %mul
+}
+
+declare double @llvm.exp.f64(double) nounwind readnone speculatable
+
+; CHECK-LABEL @exp_a_exp_b_exp_c_exp_d(
+; CHECK: fadd fast double %a, %b
+; CHECK: fadd fast double %1, %c
+; CHECK: fadd fast double %2, %d
+; CHECK: call fast double @llvm.exp.f64(double %3)
+define double @exp_a_exp_b_exp_c_exp_d(double %a, double %b, double %c, double %d) {
+  %1 = call fast double @llvm.exp.f64(double %a)
+  %2 = call fast double @llvm.exp.f64(double %b)
+  %mul = fmul fast double %1, %2
+  %3 = call fast double @llvm.exp.f64(double %c)
+  %mul1 = fmul fast double %mul, %3
+  %4 = call fast double @llvm.exp.f64(double %d)
+  %mul2 = fmul fast double %mul1, %4
+  ret double %mul2
+}
Index: lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -728,6 +728,20 @@
}
}

+  // exp(a) * exp(b) -> exp(a + b)
+  if (AllowReassociate) {
+    Value *Opnd0 = nullptr;
+    Value *Opnd1 = nullptr;
+    if (match(Op0, m_Intrinsic<Intrinsic::exp>(m_Value(Opnd0))) &&
+        match(Op1, m_Intrinsic<Intrinsic::exp>(m_Value(Opnd1)))) {
+      Builder.setFastMathFlags(I.getFastMathFlags());
+      Value *Exp = Intrinsic::getDeclaration(I.getModule(), Intrinsic::exp, I.getType());
+      Value *ExpCall = Builder.CreateCall(Exp, FAddVal);
+      return replaceInstUsesWith(I, ExpCall);
+    }
+  }
+
// Handle symmetric situation in a 2-iteration loop
Value *Opnd0 = Op0;
Value *Opnd1 = Op1;

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D41342.127307.patch
Type: text/x-patch
Size: 2237 bytes
Desc: not available