Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
123 changes: 123 additions & 0 deletions llvm/test/Transforms/InstCombine/fmul-tan-cos.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
; 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: [[RES:%.*]] = call reassoc double @llvm.sin.f64(double [[A]])
; 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: [[RES:%.*]] = call reassoc double @llvm.sin.f64(double [[A]])
; 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: [[RES:%.*]] = call reassoc double @llvm.sin.f64(double [[A]])
; 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: [[RES:%.*]] = call reassoc float @llvm.sin.f32(float [[A]])
; 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: [[RES:%.*]] = call reassoc fp128 @llvm.sin.f128(fp128 [[A]])
; 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 }
Loading