[llvm] [SimplifyLibCalls] Shrink sin, cos to sinf, cosf when allowed (PR #139082)
Guy David via llvm-commits
llvm-commits at lists.llvm.org
Thu May 8 06:47:54 PDT 2025
https://github.com/guy-david updated https://github.com/llvm/llvm-project/pull/139082
>From 31c76ddfb871e098218cbcf686c7399cafdabc1a Mon Sep 17 00:00:00 2001
From: Guy David <guyda96 at gmail.com>
Date: Thu, 8 May 2025 16:35:14 +0300
Subject: [PATCH] [SimplifyLibCalls] Shrink sin, cos to sinf, cosf when allowed
---
.../lib/Transforms/Utils/SimplifyLibCalls.cpp | 5 ++
.../InstCombine/simplify-intrinsics.ll | 69 +++++++++++++++++++
2 files changed, 74 insertions(+)
create mode 100644 llvm/test/Transforms/InstCombine/simplify-intrinsics.ll
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 941e787f91eff..94a79ad824370 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -4136,6 +4136,11 @@ Value *LibCallSimplifier::optimizeCall(CallInst *CI, IRBuilderBase &Builder) {
return optimizeMemCpy(CI, Builder);
case Intrinsic::memmove:
return optimizeMemMove(CI, Builder);
+ case Intrinsic::sin:
+ case Intrinsic::cos:
+ if (UnsafeFPShrink)
+ return optimizeUnaryDoubleFP(CI, Builder, TLI, /*isPrecise=*/true);
+ return nullptr;
default:
return nullptr;
}
diff --git a/llvm/test/Transforms/InstCombine/simplify-intrinsics.ll b/llvm/test/Transforms/InstCombine/simplify-intrinsics.ll
new file mode 100644
index 0000000000000..65888c8c80328
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/simplify-intrinsics.ll
@@ -0,0 +1,69 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s --check-prefixes=ANY,NO-FLOAT-SHRINK
+; RUN: opt < %s -passes=instcombine -enable-double-float-shrink -S | FileCheck %s --check-prefixes=ANY,DO-FLOAT-SHRINK
+
+declare double @llvm.cos.f64(double)
+declare float @llvm.cos.f32(float)
+
+declare double @llvm.sin.f64(double)
+declare float @llvm.sin.f32(float)
+
+; cos -> cosf
+
+define float @cos_no_fastmath(float %f) {
+; NO-FLOAT-SHRINK-LABEL: @cos_no_fastmath(
+; NO-FLOAT-SHRINK-NEXT: [[D:%.*]] = fpext float [[F:%.*]] to double
+; NO-FLOAT-SHRINK-NEXT: [[RESULT:%.*]] = call double @llvm.cos.f64(double [[D]])
+; NO-FLOAT-SHRINK-NEXT: [[TRUNCATED_RESULT:%.*]] = fptrunc double [[RESULT]] to float
+; NO-FLOAT-SHRINK-NEXT: ret float [[TRUNCATED_RESULT]]
+;
+; DO-FLOAT-SHRINK-LABEL: @cos_no_fastmath(
+; DO-FLOAT-SHRINK-NEXT: [[TMP1:%.*]] = call float @llvm.cos.f32(float [[F:%.*]])
+; DO-FLOAT-SHRINK-NEXT: ret float [[TMP1]]
+;
+ %d = fpext float %f to double
+ %result = call double @llvm.cos.f64(double %d)
+ %truncated_result = fptrunc double %result to float
+ ret float %truncated_result
+}
+
+define float @cos_fastmath(float %f) {
+; ANY-LABEL: @cos_fastmath(
+; ANY-NEXT: [[TMP1:%.*]] = call fast float @llvm.cos.f32(float [[F:%.*]])
+; ANY-NEXT: ret float [[TMP1]]
+;
+ %d = fpext fast float %f to double
+ %result = call fast double @llvm.cos.f64(double %d)
+ %truncated_result = fptrunc fast double %result to float
+ ret float %truncated_result
+}
+
+; sin -> sinf
+
+define float @sin_no_fastmath(float %f) {
+; NO-FLOAT-SHRINK-LABEL: @sin_no_fastmath(
+; NO-FLOAT-SHRINK-NEXT: [[D:%.*]] = fpext float [[F:%.*]] to double
+; NO-FLOAT-SHRINK-NEXT: [[RESULT:%.*]] = call double @llvm.sin.f64(double [[D]])
+; NO-FLOAT-SHRINK-NEXT: [[TRUNCATED_RESULT:%.*]] = fptrunc double [[RESULT]] to float
+; NO-FLOAT-SHRINK-NEXT: ret float [[TRUNCATED_RESULT]]
+;
+; DO-FLOAT-SHRINK-LABEL: @sin_no_fastmath(
+; DO-FLOAT-SHRINK-NEXT: [[TMP1:%.*]] = call float @llvm.sin.f32(float [[F:%.*]])
+; DO-FLOAT-SHRINK-NEXT: ret float [[TMP1]]
+;
+ %d = fpext float %f to double
+ %result = call double @llvm.sin.f64(double %d)
+ %truncated_result = fptrunc double %result to float
+ ret float %truncated_result
+}
+
+define float @sin_fastmath(float %f) {
+; ANY-LABEL: @sin_fastmath(
+; ANY-NEXT: [[TMP1:%.*]] = call fast float @llvm.sin.f32(float [[F:%.*]])
+; ANY-NEXT: ret float [[TMP1]]
+;
+ %d = fpext fast float %f to double
+ %result = call fast double @llvm.sin.f64(double %d)
+ %truncated_result = fptrunc fast double %result to float
+ ret float %truncated_result
+}
More information about the llvm-commits
mailing list