[InstCombine] Missed optimization in math expression: sin(x) / cos(x) => tan(x)

Summary: This patch enables folding sin(x) / cos(x) -> tan(x), cos(x) / sin(x) -> 1 / tan(x) under -ffast-math flag

Reviewers: hfinkel, spatel

Reviewed By: spatel

Subscribers: andrew.w.kaylor, efriedma, scanon, llvm-commits

Differential Revision: https://reviews.llvm.org/D41286

llvm-svn: 322255
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index 3860483..8abed5c 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -33,6 +33,7 @@
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/KnownBits.h"
 #include "llvm/Transforms/InstCombine/InstCombineWorklist.h"
+#include "llvm/Transforms/Utils/BuildLibCalls.h"
 #include <cassert>
 #include <cstddef>
 #include <cstdint>
@@ -1468,6 +1469,40 @@
     }
   }
 
+  if (AllowReassociate &&
+      Op0->hasOneUse() && Op1->hasOneUse()) {
+    Value *A;
+    // sin(a) / cos(a) -> tan(a)
+    if (match(Op0, m_Intrinsic<Intrinsic::sin>(m_Value(A))) &&
+        match(Op1, m_Intrinsic<Intrinsic::cos>(m_Specific(A)))) {
+      if (hasUnaryFloatFn(&TLI, I.getType(), LibFunc_tan,
+                          LibFunc_tanf, LibFunc_tanl)) {
+        IRBuilder<> B(&I);
+        IRBuilder<>::FastMathFlagGuard Guard(B);
+        B.setFastMathFlags(I.getFastMathFlags());
+        Value *Tan = emitUnaryFloatFnCall(A, TLI.getName(LibFunc_tan),
+                                          B, I.getFunction()->getAttributes());
+        return replaceInstUsesWith(I, Tan);
+      }
+    }
+
+    // cos(a) / sin(a) -> 1/tan(a)
+    if (match(Op0, m_Intrinsic<Intrinsic::cos>(m_Value(A))) &&
+        match(Op1, m_Intrinsic<Intrinsic::sin>(m_Specific(A)))) {
+      if (hasUnaryFloatFn(&TLI, I.getType(), LibFunc_tan,
+                          LibFunc_tanf, LibFunc_tanl)) {
+        IRBuilder<> B(&I);
+        IRBuilder<>::FastMathFlagGuard Guard(B);
+        B.setFastMathFlags(I.getFastMathFlags());
+        Value *Tan = emitUnaryFloatFnCall(A, TLI.getName(LibFunc_tan),
+                                          B, I.getFunction()->getAttributes());
+        Value *One = ConstantFP::get(Tan->getType(), 1.0);
+        Value *Div = B.CreateFDiv(One, Tan);
+        return replaceInstUsesWith(I, Div);
+      }
+    }
+  }
+
   Value *LHS;
   Value *RHS;