[llvm] [Transforms] Add cos(fabs(x)) -> cos(x) to SimplifyLibCalls (PR #79699)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 31 11:12:52 PST 2024
https://github.com/AtariDreams updated https://github.com/llvm/llvm-project/pull/79699
>From c00c71c03d1894862088bbb0bd42dfd0e95b7472 Mon Sep 17 00:00:00 2001
From: Rose <83477269+AtariDreams at users.noreply.github.com>
Date: Mon, 29 Jan 2024 18:56:58 -0500
Subject: [PATCH 1/2] [Transforms] Add pre-commit tests (NFC)
---
llvm/test/Transforms/InstCombine/cos-1.ll | 40 +++++++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/cos-1.ll b/llvm/test/Transforms/InstCombine/cos-1.ll
index 7cdb1d2f2b678..2bcefcb48c869 100644
--- a/llvm/test/Transforms/InstCombine/cos-1.ll
+++ b/llvm/test/Transforms/InstCombine/cos-1.ll
@@ -17,6 +17,11 @@ declare float @llvm.sin.f32(float)
declare double @tan(double)
declare fp128 @tanl(fp128)
+declare double @fabs(double)
+declare double @llvm.fabs.f64(double)
+declare float @fabsf(float)
+declare float @llvm.fabs.f32(float)
+
; cos(-x) -> cos(x);
define double @cos_negated_arg(double %x) {
@@ -100,6 +105,41 @@ define float @cosf_unary_negated_arg_FMF(float %x) {
ret float %r
}
+; cos(fabs(x)) -> cos(x)
+
+define double @cos_unary_fabs_arg(double %x) {
+; ANY-LABEL: @cos_unary_fabs_arg(
+; ANY-NEXT: [[FABS:%.*]] = tail call double @llvm.fabs.f64(double [[X:%.*]])
+; ANY-NEXT: [[R:%.*]] = call double @cos(double [[FABS]])
+; ANY-NEXT: ret double [[R]]
+;
+ %fabs = tail call double @llvm.fabs.f64(double %x)
+ %r = call double @cos(double %fabs)
+ ret double %r
+}
+
+define float @cosf_unary_fabs_arg(float %x) {
+; ANY-LABEL: @cosf_unary_fabs_arg(
+; ANY-NEXT: [[FABS:%.*]] = tail call float @llvm.fabs.f32(float [[X:%.*]])
+; ANY-NEXT: [[R:%.*]] = call float @cosf(float [[FABS]])
+; ANY-NEXT: ret float [[R]]
+;
+ %fabs = tail call float @llvm.fabs.f32(float %x)
+ %r = call float @cosf(float %fabs)
+ ret float %r
+}
+
+define float @cosf_unary_fabs_arg_FMF(float %x) {
+; ANY-LABEL: @cosf_unary_fabs_arg_FMF(
+; ANY-NEXT: [[FABS:%.*]] = tail call float @llvm.fabs.f32(float [[X:%.*]])
+; ANY-NEXT: [[R:%.*]] = call reassoc nnan float @cosf(float [[FABS]])
+; ANY-NEXT: ret float [[R]]
+;
+ %fabs = tail call float @llvm.fabs.f32(float %x)
+ %r = call nnan reassoc float @cosf(float %fabs)
+ ret float %r
+}
+
; sin(-x) -> -sin(x);
define double @sin_negated_arg(double %x) {
>From e9c4c7c961606cfffaab948099fea548c3850398 Mon Sep 17 00:00:00 2001
From: Rose <83477269+AtariDreams at users.noreply.github.com>
Date: Sat, 27 Jan 2024 12:38:19 -0500
Subject: [PATCH 2/2] [Transforms] Add cos(fabs(x)) -> cos(x) to
SimplifyLibCalls
We have this for InstCombine, but forgot to add it for SimplifyLibCalls.
---
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp | 6 ++++--
llvm/test/Transforms/InstCombine/cos-1.ll | 15 ++++++---------
2 files changed, 10 insertions(+), 11 deletions(-)
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 52eef9ab58a4d..8f1def4105f04 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -1934,8 +1934,10 @@ static Value *optimizeTrigReflections(CallInst *Call, LibFunc Func,
case LibFunc_cos:
case LibFunc_cosf:
case LibFunc_cosl:
- // cos(-X) --> cos(X)
- if (match(Call->getArgOperand(0), m_FNeg(m_Value(X))))
+ // cos(-x) --> cos(x)
+ // cos(fabs(x)) --> cos(x)
+ if (match(Call->getArgOperand(0), m_FNeg(m_Value(X))) ||
+ match(Call->getArgOperand(0), m_FAbs(m_Value(X))))
return copyFlags(*Call,
B.CreateCall(Call->getCalledFunction(), X, "cos"));
break;
diff --git a/llvm/test/Transforms/InstCombine/cos-1.ll b/llvm/test/Transforms/InstCombine/cos-1.ll
index 2bcefcb48c869..e23c269b9692c 100644
--- a/llvm/test/Transforms/InstCombine/cos-1.ll
+++ b/llvm/test/Transforms/InstCombine/cos-1.ll
@@ -109,9 +109,8 @@ define float @cosf_unary_negated_arg_FMF(float %x) {
define double @cos_unary_fabs_arg(double %x) {
; ANY-LABEL: @cos_unary_fabs_arg(
-; ANY-NEXT: [[FABS:%.*]] = tail call double @llvm.fabs.f64(double [[X:%.*]])
-; ANY-NEXT: [[R:%.*]] = call double @cos(double [[FABS]])
-; ANY-NEXT: ret double [[R]]
+; ANY-NEXT: [[COS:%.*]] = call double @cos(double [[X:%.*]])
+; ANY-NEXT: ret double [[COS]]
;
%fabs = tail call double @llvm.fabs.f64(double %x)
%r = call double @cos(double %fabs)
@@ -120,9 +119,8 @@ define double @cos_unary_fabs_arg(double %x) {
define float @cosf_unary_fabs_arg(float %x) {
; ANY-LABEL: @cosf_unary_fabs_arg(
-; ANY-NEXT: [[FABS:%.*]] = tail call float @llvm.fabs.f32(float [[X:%.*]])
-; ANY-NEXT: [[R:%.*]] = call float @cosf(float [[FABS]])
-; ANY-NEXT: ret float [[R]]
+; ANY-NEXT: [[COS:%.*]] = call float @cosf(float [[X:%.*]])
+; ANY-NEXT: ret float [[COS]]
;
%fabs = tail call float @llvm.fabs.f32(float %x)
%r = call float @cosf(float %fabs)
@@ -131,9 +129,8 @@ define float @cosf_unary_fabs_arg(float %x) {
define float @cosf_unary_fabs_arg_FMF(float %x) {
; ANY-LABEL: @cosf_unary_fabs_arg_FMF(
-; ANY-NEXT: [[FABS:%.*]] = tail call float @llvm.fabs.f32(float [[X:%.*]])
-; ANY-NEXT: [[R:%.*]] = call reassoc nnan float @cosf(float [[FABS]])
-; ANY-NEXT: ret float [[R]]
+; ANY-NEXT: [[COS:%.*]] = call reassoc nnan float @cosf(float [[X:%.*]])
+; ANY-NEXT: ret float [[COS]]
;
%fabs = tail call float @llvm.fabs.f32(float %x)
%r = call nnan reassoc float @cosf(float %fabs)
More information about the llvm-commits
mailing list