Skip to content

Commit ca7f209

Browse files
committed
[Transforms] Add cos(fabs(x)) -> cos(x) to SimplifyLibCalls
We have this for InstCombine, but forgot to add it for SimplifyLibCalls.
1 parent e550e27 commit ca7f209

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -1934,8 +1934,10 @@ static Value *optimizeTrigReflections(CallInst *Call, LibFunc Func,
19341934
case LibFunc_cos:
19351935
case LibFunc_cosf:
19361936
case LibFunc_cosl:
1937-
// cos(-X) --> cos(X)
1938-
if (match(Call->getArgOperand(0), m_FNeg(m_Value(X))))
1937+
// cos(-x) --> cos(x)
1938+
// cos(fabs(x)) --> cos(x)
1939+
if (match(Call->getArgOperand(0), m_FNeg(m_Value(X))) ||
1940+
match(Call->getArgOperand(0), m_FAbs(m_Value(X))))
19391941
return copyFlags(*Call,
19401942
B.CreateCall(Call->getCalledFunction(), X, "cos"));
19411943
break;

llvm/test/Transforms/InstCombine/cos-1.ll

+6-9
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,8 @@ define float @cosf_unary_negated_arg_FMF(float %x) {
109109

110110
define double @cos_unary_fabs_arg(double %x) {
111111
; ANY-LABEL: @cos_unary_fabs_arg(
112-
; ANY-NEXT: [[FABS:%.*]] = tail call double @llvm.fabs.f64(double [[X:%.*]])
113-
; ANY-NEXT: [[R:%.*]] = call double @cos(double [[FABS]])
114-
; ANY-NEXT: ret double [[R]]
112+
; ANY-NEXT: [[COS:%.*]] = call double @cos(double [[X:%.*]])
113+
; ANY-NEXT: ret double [[COS]]
115114
;
116115
%fabs = tail call double @llvm.fabs.f64(double %x)
117116
%r = call double @cos(double %fabs)
@@ -120,9 +119,8 @@ define double @cos_unary_fabs_arg(double %x) {
120119

121120
define float @cosf_unary_fabs_arg(float %x) {
122121
; ANY-LABEL: @cosf_unary_fabs_arg(
123-
; ANY-NEXT: [[FABS:%.*]] = tail call float @llvm.fabs.f32(float [[X:%.*]])
124-
; ANY-NEXT: [[R:%.*]] = call float @cosf(float [[FABS]])
125-
; ANY-NEXT: ret float [[R]]
122+
; ANY-NEXT: [[COS:%.*]] = call float @cosf(float [[X:%.*]])
123+
; ANY-NEXT: ret float [[COS]]
126124
;
127125
%fabs = tail call float @llvm.fabs.f32(float %x)
128126
%r = call float @cosf(float %fabs)
@@ -131,9 +129,8 @@ define float @cosf_unary_fabs_arg(float %x) {
131129

132130
define float @cosf_unary_fabs_arg_FMF(float %x) {
133131
; ANY-LABEL: @cosf_unary_fabs_arg_FMF(
134-
; ANY-NEXT: [[FABS:%.*]] = tail call float @llvm.fabs.f32(float [[X:%.*]])
135-
; ANY-NEXT: [[R:%.*]] = call reassoc nnan float @cosf(float [[FABS]])
136-
; ANY-NEXT: ret float [[R]]
132+
; ANY-NEXT: [[COS:%.*]] = call reassoc nnan float @cosf(float [[X:%.*]])
133+
; ANY-NEXT: ret float [[COS]]
137134
;
138135
%fabs = tail call float @llvm.fabs.f32(float %x)
139136
%r = call nnan reassoc float @cosf(float %fabs)

0 commit comments

Comments
 (0)