Skip to content

[Transforms] Add more cos combinations to SimplifyLibCalls and InstCombine #79699

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

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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: 6 additions & 4 deletions llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2488,11 +2488,13 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
}
case Intrinsic::cos:
case Intrinsic::amdgcn_cos: {
Value *X;
Value *X, *Sign;
Value *Src = II->getArgOperand(0);
if (match(Src, m_FNeg(m_Value(X))) || match(Src, m_FAbs(m_Value(X)))) {
// cos(-x) -> cos(x)
// cos(fabs(x)) -> cos(x)
if (match(Src, m_FNeg(m_Value(X))) || match(Src, m_FAbs(m_Value(X))) ||
match(Src, m_CopySign(m_Value(X), m_Value(Sign)))) {
// cos(-x) --> cos(x)
// cos(fabs(x)) --> cos(x)
// cos(copysign(x, y)) --> cos(x)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be broken out into an optimizeEvenFunction helper method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do that in another PR. I think this is good for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In InstCombineAndOrXor we have stripSignOnlyFPOps

return replaceOperand(*II, 0, X);
}
break;
Expand Down
12 changes: 9 additions & 3 deletions llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1933,12 +1933,18 @@ static Value *optimizeTrigReflections(CallInst *Call, LibFunc Func,
break;
case LibFunc_cos:
case LibFunc_cosf:
case LibFunc_cosl:
// cos(-X) --> cos(X)
if (match(Call->getArgOperand(0), m_FNeg(m_Value(X))))
case LibFunc_cosl: {
// cos(-x) --> cos(x)
// cos(fabs(x)) --> cos(x)
// cos(copysign(x, y)) --> cos(x)
Value *Sign;
Value *Src = Call->getArgOperand(0);
if (match(Src, m_FNeg(m_Value(X))) || match(Src, m_FAbs(m_Value(X))) ||
match(Src, m_CopySign(m_Value(X), m_Value(Sign))))
return copyFlags(*Call,
B.CreateCall(Call->getCalledFunction(), X, "cos"));
break;
}
default:
break;
}
Expand Down
73 changes: 73 additions & 0 deletions llvm/test/Transforms/InstCombine/cos-1.ll
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ 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)

declare double @llvm.copysign(double, double)
declare float @llvm.copysign.f32(float, float)

; cos(-x) -> cos(x);

define double @cos_negated_arg(double %x) {
Expand Down Expand Up @@ -100,6 +108,71 @@ 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: [[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)
ret double %r
}

define float @cosf_unary_fabs_arg(float %x) {
; ANY-LABEL: @cosf_unary_fabs_arg(
; 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)
ret float %r
}

define float @cosf_unary_fabs_arg_FMF(float %x) {
; ANY-LABEL: @cosf_unary_fabs_arg_FMF(
; 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)
ret float %r
}

; cos(copysign(x, y)) -> cos(x)

define double @cos_copysign_arg(double %x, double %y) {
; ANY-LABEL: @cos_copysign_arg(
; ANY-NEXT: [[COS:%.*]] = call double @cos(double [[X:%.*]])
; ANY-NEXT: ret double [[COS]]
;
%copysign = tail call double @llvm.copysign(double %x, double %y)
%r = call double @cos(double %copysign)
ret double %r
}


define float @cosf_unary_copysign_arg(float %x) {
; ANY-LABEL: @cosf_unary_copysign_arg(
; ANY-NEXT: [[COS:%.*]] = call float @cosf(float [[X:%.*]])
; ANY-NEXT: ret float [[COS]]
;
%copysign = tail call float @llvm.copysign.f32(float %x, float 1.0)
%r = call float @cosf(float %copysign)
ret float %r
}

define float @cosf_copysign_arg_FMF(float %x, float %y) {
; ANY-LABEL: @cosf_copysign_arg_FMF(
; ANY-NEXT: [[COS:%.*]] = call reassoc nnan float @cosf(float [[X:%.*]])
; ANY-NEXT: ret float [[COS]]
;
%copysign = tail call float @llvm.copysign.f32(float %x, float %y)
%r = call nnan reassoc float @cosf(float %copysign)
ret float %r
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test a vector intrinsic case?

; sin(-x) -> -sin(x);

define double @sin_negated_arg(double %x) {
Expand Down