[llvm] [InstCombine] Fold tan(x) * cos(x) => sin(x) (PR #136319)

via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 19 08:31:57 PDT 2025


https://github.com/amordo updated https://github.com/llvm/llvm-project/pull/136319

>From 1f2fb834c5dfb3bf7b5f902ba134ed21fcbdebcb Mon Sep 17 00:00:00 2001
From: Alexander Mordovskiy <iammorjj at gmail.com>
Date: Fri, 18 Apr 2025 17:35:20 +0200
Subject: [PATCH 1/3] [InstCombine] Pre-commit tests (NFC)

---
 .../Transforms/InstCombine/fmul-tan-cos.ll    | 133 ++++++++++++++++++
 1 file changed, 133 insertions(+)
 create mode 100644 llvm/test/Transforms/InstCombine/fmul-tan-cos.ll

diff --git a/llvm/test/Transforms/InstCombine/fmul-tan-cos.ll b/llvm/test/Transforms/InstCombine/fmul-tan-cos.ll
new file mode 100644
index 0000000000000..3e79eed90c330
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/fmul-tan-cos.ll
@@ -0,0 +1,133 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -passes=instcombine < %s | FileCheck %s
+
+define double @fmul_tan_cos(double %a) {
+; CHECK-LABEL: define double @fmul_tan_cos(
+; CHECK-SAME: double [[A:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.tan.f64(double [[A]])
+; CHECK-NEXT:    [[TMP2:%.*]] = call double @llvm.cos.f64(double [[A]])
+; CHECK-NEXT:    [[RES:%.*]] = fmul double [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    ret double [[RES]]
+;
+  %1 = call double @llvm.tan.f64(double %a)
+  %2 = call double @llvm.cos.f64(double %a)
+  %res = fmul double %1, %2
+  ret double %res
+}
+
+define double @fmul_strict_tan_strict_cos_reassoc(double %a) {
+; CHECK-LABEL: define double @fmul_strict_tan_strict_cos_reassoc(
+; CHECK-SAME: double [[A:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.tan.f64(double [[A]])
+; CHECK-NEXT:    [[TMP2:%.*]] = call reassoc double @llvm.cos.f64(double [[A]])
+; CHECK-NEXT:    [[RES:%.*]] = fmul double [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    ret double [[RES]]
+;
+  %1 = call double @llvm.tan.f64(double %a)
+  %2 = call reassoc double @llvm.cos.f64(double %a)
+  %res = fmul double %1, %2
+  ret double %res
+}
+
+define double @fmul_reassoc_tan_strict_cos_strict(double %a, ptr dereferenceable(2) %dummy) {
+; CHECK-LABEL: define double @fmul_reassoc_tan_strict_cos_strict(
+; CHECK-SAME: double [[A:%.*]], ptr dereferenceable(2) [[DUMMY:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.tan.f64(double [[A]])
+; CHECK-NEXT:    [[TMP2:%.*]] = call double @llvm.cos.f64(double [[A]])
+; CHECK-NEXT:    [[RES:%.*]] = fmul reassoc double [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    ret double [[RES]]
+;
+  %1 = call double @llvm.tan.f64(double %a)
+  %2 = call double @llvm.cos.f64(double %a)
+  %res = fmul reassoc double %1, %2
+  ret double %res
+}
+
+define double @fmul_reassoc_tan_reassoc_cos_strict(double %a) {
+; CHECK-LABEL: define double @fmul_reassoc_tan_reassoc_cos_strict(
+; CHECK-SAME: double [[A:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call reassoc double @llvm.tan.f64(double [[A]])
+; CHECK-NEXT:    [[TMP2:%.*]] = call double @llvm.cos.f64(double [[A]])
+; CHECK-NEXT:    [[RES:%.*]] = fmul reassoc double [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    ret double [[RES]]
+;
+  %1 = call reassoc double @llvm.tan.f64(double %a)
+  %2 = call double @llvm.cos.f64(double %a)
+  %res = fmul reassoc double %1, %2
+  ret double %res
+}
+
+define double @fmul_tan_cos_reassoc_multiple_uses(double %a) {
+; CHECK-LABEL: define double @fmul_tan_cos_reassoc_multiple_uses(
+; CHECK-SAME: double [[A:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call reassoc double @llvm.tan.f64(double [[A]])
+; CHECK-NEXT:    [[TMP2:%.*]] = call reassoc double @llvm.cos.f64(double [[A]])
+; CHECK-NEXT:    [[RES:%.*]] = fmul reassoc double [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    call void @use(double [[TMP2]])
+; CHECK-NEXT:    ret double [[RES]]
+;
+  %1 = call reassoc double @llvm.tan.f64(double %a)
+  %2 = call reassoc double @llvm.cos.f64(double %a)
+  %res = fmul reassoc double %1, %2
+  call void @use(double %2)
+  ret double %res
+}
+
+define double @fmul_tan_cos_reassoc(double %a) {
+; CHECK-LABEL: define double @fmul_tan_cos_reassoc(
+; CHECK-SAME: double [[A:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call reassoc double @llvm.tan.f64(double [[A]])
+; CHECK-NEXT:    [[TMP2:%.*]] = call reassoc double @llvm.cos.f64(double [[A]])
+; CHECK-NEXT:    [[RES:%.*]] = fmul reassoc double [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    ret double [[RES]]
+;
+  %1 = call reassoc double @llvm.tan.f64(double %a)
+  %2 = call reassoc double @llvm.cos.f64(double %a)
+  %res = fmul reassoc double %1, %2
+  ret double %res
+}
+
+define float @fmul_tanf_cosf_reassoc(float %a) {
+; CHECK-LABEL: define float @fmul_tanf_cosf_reassoc(
+; CHECK-SAME: float [[A:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call reassoc float @llvm.tan.f32(float [[A]])
+; CHECK-NEXT:    [[TMP2:%.*]] = call reassoc float @llvm.cos.f32(float [[A]])
+; CHECK-NEXT:    [[RES:%.*]] = fmul reassoc float [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    ret float [[RES]]
+;
+  %1 = call reassoc float @llvm.tan.f32(float %a)
+  %2 = call reassoc float @llvm.cos.f32(float %a)
+  %res = fmul reassoc float %1, %2
+  ret float %res
+}
+
+define fp128 @fmul_tanfp128_cosfp128_reassoc(fp128 %a) {
+; CHECK-LABEL: define fp128 @fmul_tanfp128_cosfp128_reassoc(
+; CHECK-SAME: fp128 [[A:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call reassoc fp128 @llvm.tan.f128(fp128 [[A]])
+; CHECK-NEXT:    [[TMP2:%.*]] = call reassoc fp128 @llvm.cos.f128(fp128 [[A]])
+; CHECK-NEXT:    [[RES:%.*]] = fmul reassoc fp128 [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    ret fp128 [[RES]]
+;
+  %1 = call reassoc fp128 @llvm.tan.fp128(fp128 %a)
+  %2 = call reassoc fp128 @llvm.cos.fp128(fp128 %a)
+  %res = fmul reassoc fp128 %1, %2
+  ret fp128 %res
+}
+
+declare double @llvm.sin.f64(double) #1
+declare float @llvm.sin.f32(float) #1
+declare fp128 @llvm.sin.fp128(fp128) #1
+
+declare double @llvm.cos.f64(double) #1
+declare float @llvm.cos.f32(float) #1
+declare fp128 @llvm.cos.fp128(fp128) #1
+
+declare double @llvm.tan.f64(double) #1
+declare float @llvm.tan.f32(float) #1
+declare fp128 @llvm.tan.fp128(fp128) #1
+
+declare void @use(double)
+
+attributes #0 = { nounwind readnone speculatable }
+attributes #1 = { nounwind readnone }

>From f06b8aa2e4c5b02e8f3a4213a03fae8d95aea408 Mon Sep 17 00:00:00 2001
From: Alexander Mordovskiy <iammorjj at gmail.com>
Date: Fri, 18 Apr 2025 17:38:46 +0200
Subject: [PATCH 2/3] [InstCombine] Fold tan(x) * cos(x) => sin(x)

Summary: This patch enables folding tan(x) * cos(x) -> sin(x) under -ffast-math flag
---
 .../InstCombine/InstCombineMulDivRem.cpp      | 10 ++++++++++
 .../Transforms/InstCombine/fmul-tan-cos.ll    | 20 +++++--------------
 2 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index c7023eb79b04e..e3ba0c4468d92 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1073,6 +1073,16 @@ Instruction *InstCombinerImpl::visitFMul(BinaryOperator &I) {
     return Result;
   }
 
+  // tan(X) * cos(X) -> sin(X)
+  if (I.hasAllowReassoc() && Op0->hasOneUse() && Op1->hasOneUse()) {
+    Value *X;
+    if (match(Op0, m_Intrinsic<Intrinsic::tan>(m_Value(X))) &&
+        match(Op1, m_Intrinsic<Intrinsic::cos>(m_Specific(X)))) {
+      Value *Sin = Builder.CreateUnaryIntrinsic(Intrinsic::sin, X, &I);
+      return replaceInstUsesWith(I, Sin);
+    }
+  }
+
   return nullptr;
 }
 
diff --git a/llvm/test/Transforms/InstCombine/fmul-tan-cos.ll b/llvm/test/Transforms/InstCombine/fmul-tan-cos.ll
index 3e79eed90c330..8196e8f7c81ba 100644
--- a/llvm/test/Transforms/InstCombine/fmul-tan-cos.ll
+++ b/llvm/test/Transforms/InstCombine/fmul-tan-cos.ll
@@ -32,9 +32,7 @@ define double @fmul_strict_tan_strict_cos_reassoc(double %a) {
 define double @fmul_reassoc_tan_strict_cos_strict(double %a, ptr dereferenceable(2) %dummy) {
 ; CHECK-LABEL: define double @fmul_reassoc_tan_strict_cos_strict(
 ; CHECK-SAME: double [[A:%.*]], ptr dereferenceable(2) [[DUMMY:%.*]]) {
-; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.tan.f64(double [[A]])
-; CHECK-NEXT:    [[TMP2:%.*]] = call double @llvm.cos.f64(double [[A]])
-; CHECK-NEXT:    [[RES:%.*]] = fmul reassoc double [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    [[RES:%.*]] = call reassoc double @llvm.sin.f64(double [[A]])
 ; CHECK-NEXT:    ret double [[RES]]
 ;
   %1 = call double @llvm.tan.f64(double %a)
@@ -46,9 +44,7 @@ define double @fmul_reassoc_tan_strict_cos_strict(double %a, ptr dereferenceable
 define double @fmul_reassoc_tan_reassoc_cos_strict(double %a) {
 ; CHECK-LABEL: define double @fmul_reassoc_tan_reassoc_cos_strict(
 ; CHECK-SAME: double [[A:%.*]]) {
-; CHECK-NEXT:    [[TMP1:%.*]] = call reassoc double @llvm.tan.f64(double [[A]])
-; CHECK-NEXT:    [[TMP2:%.*]] = call double @llvm.cos.f64(double [[A]])
-; CHECK-NEXT:    [[RES:%.*]] = fmul reassoc double [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    [[RES:%.*]] = call reassoc double @llvm.sin.f64(double [[A]])
 ; CHECK-NEXT:    ret double [[RES]]
 ;
   %1 = call reassoc double @llvm.tan.f64(double %a)
@@ -76,9 +72,7 @@ define double @fmul_tan_cos_reassoc_multiple_uses(double %a) {
 define double @fmul_tan_cos_reassoc(double %a) {
 ; CHECK-LABEL: define double @fmul_tan_cos_reassoc(
 ; CHECK-SAME: double [[A:%.*]]) {
-; CHECK-NEXT:    [[TMP1:%.*]] = call reassoc double @llvm.tan.f64(double [[A]])
-; CHECK-NEXT:    [[TMP2:%.*]] = call reassoc double @llvm.cos.f64(double [[A]])
-; CHECK-NEXT:    [[RES:%.*]] = fmul reassoc double [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    [[RES:%.*]] = call reassoc double @llvm.sin.f64(double [[A]])
 ; CHECK-NEXT:    ret double [[RES]]
 ;
   %1 = call reassoc double @llvm.tan.f64(double %a)
@@ -90,9 +84,7 @@ define double @fmul_tan_cos_reassoc(double %a) {
 define float @fmul_tanf_cosf_reassoc(float %a) {
 ; CHECK-LABEL: define float @fmul_tanf_cosf_reassoc(
 ; CHECK-SAME: float [[A:%.*]]) {
-; CHECK-NEXT:    [[TMP1:%.*]] = call reassoc float @llvm.tan.f32(float [[A]])
-; CHECK-NEXT:    [[TMP2:%.*]] = call reassoc float @llvm.cos.f32(float [[A]])
-; CHECK-NEXT:    [[RES:%.*]] = fmul reassoc float [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    [[RES:%.*]] = call reassoc float @llvm.sin.f32(float [[A]])
 ; CHECK-NEXT:    ret float [[RES]]
 ;
   %1 = call reassoc float @llvm.tan.f32(float %a)
@@ -104,9 +96,7 @@ define float @fmul_tanf_cosf_reassoc(float %a) {
 define fp128 @fmul_tanfp128_cosfp128_reassoc(fp128 %a) {
 ; CHECK-LABEL: define fp128 @fmul_tanfp128_cosfp128_reassoc(
 ; CHECK-SAME: fp128 [[A:%.*]]) {
-; CHECK-NEXT:    [[TMP1:%.*]] = call reassoc fp128 @llvm.tan.f128(fp128 [[A]])
-; CHECK-NEXT:    [[TMP2:%.*]] = call reassoc fp128 @llvm.cos.f128(fp128 [[A]])
-; CHECK-NEXT:    [[RES:%.*]] = fmul reassoc fp128 [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    [[RES:%.*]] = call reassoc fp128 @llvm.sin.f128(fp128 [[A]])
 ; CHECK-NEXT:    ret fp128 [[RES]]
 ;
   %1 = call reassoc fp128 @llvm.tan.fp128(fp128 %a)

>From 3ce32a0cb1ba223ac647db315c568d516c0cb918 Mon Sep 17 00:00:00 2001
From: Alexander Mordovskiy <iammorjj at gmail.com>
Date: Sat, 19 Apr 2025 17:28:47 +0200
Subject: [PATCH 3/3] Apply recommendations

Moved the folding into InstCombinerImpl::foldFMulReassoc()
---
 .../InstCombine/InstCombineMulDivRem.cpp      |  18 ++-
 .../Transforms/InstCombine/fmul-tan-cos.ll    | 109 +++++++++---------
 2 files changed, 60 insertions(+), 67 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index e3ba0c4468d92..7a946d5ffd6d5 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -947,6 +947,14 @@ Instruction *InstCombinerImpl::foldFMulReassoc(BinaryOperator &I) {
     return BinaryOperator::CreateFMulFMF(XX, Y, &I);
   }
 
+  // tan(X) * cos(X) -> sin(X)
+  if (match(&I,
+            m_c_FMul(m_OneUse(m_Intrinsic<Intrinsic::tan>(m_Value(X))),
+                     m_OneUse(m_Intrinsic<Intrinsic::cos>(m_Specific(X)))))) {
+    Value *Sin = Builder.CreateUnaryIntrinsic(Intrinsic::sin, X, &I);
+    return replaceInstUsesWith(I, Sin);
+  }
+
   return nullptr;
 }
 
@@ -1073,16 +1081,6 @@ Instruction *InstCombinerImpl::visitFMul(BinaryOperator &I) {
     return Result;
   }
 
-  // tan(X) * cos(X) -> sin(X)
-  if (I.hasAllowReassoc() && Op0->hasOneUse() && Op1->hasOneUse()) {
-    Value *X;
-    if (match(Op0, m_Intrinsic<Intrinsic::tan>(m_Value(X))) &&
-        match(Op1, m_Intrinsic<Intrinsic::cos>(m_Specific(X)))) {
-      Value *Sin = Builder.CreateUnaryIntrinsic(Intrinsic::sin, X, &I);
-      return replaceInstUsesWith(I, Sin);
-    }
-  }
-
   return nullptr;
 }
 
diff --git a/llvm/test/Transforms/InstCombine/fmul-tan-cos.ll b/llvm/test/Transforms/InstCombine/fmul-tan-cos.ll
index 8196e8f7c81ba..3c2d3825cc122 100644
--- a/llvm/test/Transforms/InstCombine/fmul-tan-cos.ll
+++ b/llvm/test/Transforms/InstCombine/fmul-tan-cos.ll
@@ -4,120 +4,115 @@
 define double @fmul_tan_cos(double %a) {
 ; CHECK-LABEL: define double @fmul_tan_cos(
 ; CHECK-SAME: double [[A:%.*]]) {
-; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.tan.f64(double [[A]])
-; CHECK-NEXT:    [[TMP2:%.*]] = call double @llvm.cos.f64(double [[A]])
-; CHECK-NEXT:    [[RES:%.*]] = fmul double [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    [[TAN:%.*]] = call double @llvm.tan.f64(double [[A]])
+; CHECK-NEXT:    [[COS:%.*]] = call double @llvm.cos.f64(double [[A]])
+; CHECK-NEXT:    [[RES:%.*]] = fmul double [[TAN]], [[COS]]
 ; CHECK-NEXT:    ret double [[RES]]
 ;
-  %1 = call double @llvm.tan.f64(double %a)
-  %2 = call double @llvm.cos.f64(double %a)
-  %res = fmul double %1, %2
+  %tan = call double @llvm.tan.f64(double %a)
+  %cos = call double @llvm.cos.f64(double %a)
+  %res = fmul double %tan, %cos
   ret double %res
 }
 
 define double @fmul_strict_tan_strict_cos_reassoc(double %a) {
 ; CHECK-LABEL: define double @fmul_strict_tan_strict_cos_reassoc(
 ; CHECK-SAME: double [[A:%.*]]) {
-; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.tan.f64(double [[A]])
-; CHECK-NEXT:    [[TMP2:%.*]] = call reassoc double @llvm.cos.f64(double [[A]])
-; CHECK-NEXT:    [[RES:%.*]] = fmul double [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    [[TAN:%.*]] = call double @llvm.tan.f64(double [[A]])
+; CHECK-NEXT:    [[COS:%.*]] = call reassoc double @llvm.cos.f64(double [[A]])
+; CHECK-NEXT:    [[RES:%.*]] = fmul double [[TAN]], [[COS]]
 ; CHECK-NEXT:    ret double [[RES]]
 ;
-  %1 = call double @llvm.tan.f64(double %a)
-  %2 = call reassoc double @llvm.cos.f64(double %a)
-  %res = fmul double %1, %2
+  %tan = call double @llvm.tan.f64(double %a)
+  %cos = call reassoc double @llvm.cos.f64(double %a)
+  %res = fmul double %tan, %cos
   ret double %res
 }
 
-define double @fmul_reassoc_tan_strict_cos_strict(double %a, ptr dereferenceable(2) %dummy) {
+define double @fmul_reassoc_tan_strict_cos_strict(double %a) {
 ; CHECK-LABEL: define double @fmul_reassoc_tan_strict_cos_strict(
-; CHECK-SAME: double [[A:%.*]], ptr dereferenceable(2) [[DUMMY:%.*]]) {
-; CHECK-NEXT:    [[RES:%.*]] = call reassoc double @llvm.sin.f64(double [[A]])
+; CHECK-SAME: double [[A:%.*]]) {
+; CHECK-NEXT:    [[TAN:%.*]] = call double @llvm.tan.f64(double [[A]])
+; CHECK-NEXT:    [[COS:%.*]] = call double @llvm.cos.f64(double [[A]])
+; CHECK-NEXT:    [[RES:%.*]] = fmul reassoc double [[TAN]], [[COS]]
 ; CHECK-NEXT:    ret double [[RES]]
 ;
-  %1 = call double @llvm.tan.f64(double %a)
-  %2 = call double @llvm.cos.f64(double %a)
-  %res = fmul reassoc double %1, %2
+  %tan = call double @llvm.tan.f64(double %a)
+  %cos = call double @llvm.cos.f64(double %a)
+  %res = fmul reassoc double %tan, %cos
   ret double %res
 }
 
 define double @fmul_reassoc_tan_reassoc_cos_strict(double %a) {
 ; CHECK-LABEL: define double @fmul_reassoc_tan_reassoc_cos_strict(
 ; CHECK-SAME: double [[A:%.*]]) {
-; CHECK-NEXT:    [[RES:%.*]] = call reassoc double @llvm.sin.f64(double [[A]])
+; CHECK-NEXT:    [[TAN:%.*]] = call reassoc double @llvm.tan.f64(double [[A]])
+; CHECK-NEXT:    [[COS:%.*]] = call double @llvm.cos.f64(double [[A]])
+; CHECK-NEXT:    [[RES:%.*]] = fmul reassoc double [[TAN]], [[COS]]
 ; CHECK-NEXT:    ret double [[RES]]
 ;
-  %1 = call reassoc double @llvm.tan.f64(double %a)
-  %2 = call double @llvm.cos.f64(double %a)
-  %res = fmul reassoc double %1, %2
+  %tan = call reassoc double @llvm.tan.f64(double %a)
+  %cos = call double @llvm.cos.f64(double %a)
+  %res = fmul reassoc double %tan, %cos
   ret double %res
 }
 
 define double @fmul_tan_cos_reassoc_multiple_uses(double %a) {
 ; CHECK-LABEL: define double @fmul_tan_cos_reassoc_multiple_uses(
 ; CHECK-SAME: double [[A:%.*]]) {
-; CHECK-NEXT:    [[TMP1:%.*]] = call reassoc double @llvm.tan.f64(double [[A]])
-; CHECK-NEXT:    [[TMP2:%.*]] = call reassoc double @llvm.cos.f64(double [[A]])
-; CHECK-NEXT:    [[RES:%.*]] = fmul reassoc double [[TMP1]], [[TMP2]]
-; CHECK-NEXT:    call void @use(double [[TMP2]])
+; CHECK-NEXT:    [[TAN:%.*]] = call reassoc double @llvm.tan.f64(double [[A]])
+; CHECK-NEXT:    [[COS:%.*]] = call reassoc double @llvm.cos.f64(double [[A]])
+; CHECK-NEXT:    [[RES:%.*]] = fmul reassoc double [[TAN]], [[COS]]
+; CHECK-NEXT:    call void @use(double [[COS]])
 ; CHECK-NEXT:    ret double [[RES]]
 ;
-  %1 = call reassoc double @llvm.tan.f64(double %a)
-  %2 = call reassoc double @llvm.cos.f64(double %a)
-  %res = fmul reassoc double %1, %2
-  call void @use(double %2)
+  %tan = call reassoc double @llvm.tan.f64(double %a)
+  %cos = call reassoc double @llvm.cos.f64(double %a)
+  %res = fmul reassoc double %tan, %cos
+  call void @use(double %cos)
   ret double %res
 }
 
 define double @fmul_tan_cos_reassoc(double %a) {
 ; CHECK-LABEL: define double @fmul_tan_cos_reassoc(
 ; CHECK-SAME: double [[A:%.*]]) {
-; CHECK-NEXT:    [[RES:%.*]] = call reassoc double @llvm.sin.f64(double [[A]])
+; CHECK-NEXT:    [[TAN:%.*]] = call reassoc double @llvm.tan.f64(double [[A]])
+; CHECK-NEXT:    [[COS:%.*]] = call reassoc double @llvm.cos.f64(double [[A]])
+; CHECK-NEXT:    [[RES:%.*]] = fmul reassoc double [[TAN]], [[COS]]
 ; CHECK-NEXT:    ret double [[RES]]
 ;
-  %1 = call reassoc double @llvm.tan.f64(double %a)
-  %2 = call reassoc double @llvm.cos.f64(double %a)
-  %res = fmul reassoc double %1, %2
+  %tan = call reassoc double @llvm.tan.f64(double %a)
+  %cos = call reassoc double @llvm.cos.f64(double %a)
+  %res = fmul reassoc double %tan, %cos
   ret double %res
 }
 
 define float @fmul_tanf_cosf_reassoc(float %a) {
 ; CHECK-LABEL: define float @fmul_tanf_cosf_reassoc(
 ; CHECK-SAME: float [[A:%.*]]) {
-; CHECK-NEXT:    [[RES:%.*]] = call reassoc float @llvm.sin.f32(float [[A]])
+; CHECK-NEXT:    [[TAN:%.*]] = call reassoc float @llvm.tan.f32(float [[A]])
+; CHECK-NEXT:    [[COS:%.*]] = call reassoc float @llvm.cos.f32(float [[A]])
+; CHECK-NEXT:    [[RES:%.*]] = fmul reassoc float [[TAN]], [[COS]]
 ; CHECK-NEXT:    ret float [[RES]]
 ;
-  %1 = call reassoc float @llvm.tan.f32(float %a)
-  %2 = call reassoc float @llvm.cos.f32(float %a)
-  %res = fmul reassoc float %1, %2
+  %tan = call reassoc float @llvm.tan.f32(float %a)
+  %cos = call reassoc float @llvm.cos.f32(float %a)
+  %res = fmul reassoc float %tan, %cos
   ret float %res
 }
 
 define fp128 @fmul_tanfp128_cosfp128_reassoc(fp128 %a) {
 ; CHECK-LABEL: define fp128 @fmul_tanfp128_cosfp128_reassoc(
 ; CHECK-SAME: fp128 [[A:%.*]]) {
-; CHECK-NEXT:    [[RES:%.*]] = call reassoc fp128 @llvm.sin.f128(fp128 [[A]])
+; CHECK-NEXT:    [[TAN:%.*]] = call reassoc fp128 @llvm.tan.f128(fp128 [[A]])
+; CHECK-NEXT:    [[COS:%.*]] = call reassoc fp128 @llvm.cos.f128(fp128 [[A]])
+; CHECK-NEXT:    [[RES:%.*]] = fmul reassoc fp128 [[TAN]], [[COS]]
 ; CHECK-NEXT:    ret fp128 [[RES]]
 ;
-  %1 = call reassoc fp128 @llvm.tan.fp128(fp128 %a)
-  %2 = call reassoc fp128 @llvm.cos.fp128(fp128 %a)
-  %res = fmul reassoc fp128 %1, %2
+  %tan = call reassoc fp128 @llvm.tan.fp128(fp128 %a)
+  %cos = call reassoc fp128 @llvm.cos.fp128(fp128 %a)
+  %res = fmul reassoc fp128 %tan, %cos
   ret fp128 %res
 }
 
-declare double @llvm.sin.f64(double) #1
-declare float @llvm.sin.f32(float) #1
-declare fp128 @llvm.sin.fp128(fp128) #1
-
-declare double @llvm.cos.f64(double) #1
-declare float @llvm.cos.f32(float) #1
-declare fp128 @llvm.cos.fp128(fp128) #1
-
-declare double @llvm.tan.f64(double) #1
-declare float @llvm.tan.f32(float) #1
-declare fp128 @llvm.tan.fp128(fp128) #1
-
 declare void @use(double)
-
-attributes #0 = { nounwind readnone speculatable }
-attributes #1 = { nounwind readnone }



More information about the llvm-commits mailing list