Skip to content

Commit 6fa4477

Browse files
committed
[Transforms] Add more cos combinations to SimplifyLibCalls and InstCombineCalls
Add cos(fabs(x)) -> cos(x) and cos(copysign(x, y)) -> cos(x).
1 parent b74d412 commit 6fa4477

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2488,11 +2488,13 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
24882488
}
24892489
case Intrinsic::cos:
24902490
case Intrinsic::amdgcn_cos: {
2491-
Value *X;
2491+
Value *X, *Sign;
24922492
Value *Src = II->getArgOperand(0);
2493-
if (match(Src, m_FNeg(m_Value(X))) || match(Src, m_FAbs(m_Value(X)))) {
2494-
// cos(-x) -> cos(x)
2495-
// cos(fabs(x)) -> cos(x)
2493+
if (match(Src, m_FNeg(m_Value(X))) || match(Src, m_FAbs(m_Value(X))) ||
2494+
match(Src, m_CopySign(m_Value(X), m_Value(Sign)))) {
2495+
// cos(-x) --> cos(x)
2496+
// cos(fabs(x)) --> cos(x)
2497+
// cos(copysign(x, y)) --> cos(x)
24962498
return replaceOperand(*II, 0, X);
24972499
}
24982500
break;

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,12 +1933,18 @@ static Value *optimizeTrigReflections(CallInst *Call, LibFunc Func,
19331933
break;
19341934
case LibFunc_cos:
19351935
case LibFunc_cosf:
1936-
case LibFunc_cosl:
1937-
// cos(-X) --> cos(X)
1938-
if (match(Call->getArgOperand(0), m_FNeg(m_Value(X))))
1936+
case LibFunc_cosl: {
1937+
// cos(-x) --> cos(x)
1938+
// cos(fabs(x)) --> cos(x)
1939+
// cos(copysign(x, y)) --> cos(x)
1940+
Value *Sign;
1941+
if (match(Call->getArgOperand(0), m_FNeg(m_Value(X))) ||
1942+
match(Call->getArgOperand(0), m_FAbs(m_Value(X))) ||
1943+
match(Call->getArgOperand(0), m_CopySign(m_Value(X), m_Value(Sign))))
19391944
return copyFlags(*Call,
19401945
B.CreateCall(Call->getCalledFunction(), X, "cos"));
19411946
break;
1947+
}
19421948
default:
19431949
break;
19441950
}

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,8 @@ define float @cosf_unary_negated_arg_FMF(float %x) {
112112

113113
define double @cos_unary_fabs_arg(double %x) {
114114
; ANY-LABEL: @cos_unary_fabs_arg(
115-
; ANY-NEXT: [[FABS:%.*]] = tail call double @llvm.fabs.f64(double [[X:%.*]])
116-
; ANY-NEXT: [[R:%.*]] = call double @cos(double [[FABS]])
117-
; ANY-NEXT: ret double [[R]]
115+
; ANY-NEXT: [[COS:%.*]] = call double @cos(double [[X:%.*]])
116+
; ANY-NEXT: ret double [[COS]]
118117
;
119118
%fabs = tail call double @llvm.fabs.f64(double %x)
120119
%r = call double @cos(double %fabs)
@@ -123,9 +122,8 @@ define double @cos_unary_fabs_arg(double %x) {
123122

124123
define float @cosf_unary_fabs_arg(float %x) {
125124
; ANY-LABEL: @cosf_unary_fabs_arg(
126-
; ANY-NEXT: [[FABS:%.*]] = tail call float @llvm.fabs.f32(float [[X:%.*]])
127-
; ANY-NEXT: [[R:%.*]] = call float @cosf(float [[FABS]])
128-
; ANY-NEXT: ret float [[R]]
125+
; ANY-NEXT: [[COS:%.*]] = call float @cosf(float [[X:%.*]])
126+
; ANY-NEXT: ret float [[COS]]
129127
;
130128
%fabs = tail call float @llvm.fabs.f32(float %x)
131129
%r = call float @cosf(float %fabs)
@@ -134,9 +132,8 @@ define float @cosf_unary_fabs_arg(float %x) {
134132

135133
define float @cosf_unary_fabs_arg_FMF(float %x) {
136134
; ANY-LABEL: @cosf_unary_fabs_arg_FMF(
137-
; ANY-NEXT: [[FABS:%.*]] = tail call float @llvm.fabs.f32(float [[X:%.*]])
138-
; ANY-NEXT: [[R:%.*]] = call reassoc nnan float @cosf(float [[FABS]])
139-
; ANY-NEXT: ret float [[R]]
135+
; ANY-NEXT: [[COS:%.*]] = call reassoc nnan float @cosf(float [[X:%.*]])
136+
; ANY-NEXT: ret float [[COS]]
140137
;
141138
%fabs = tail call float @llvm.fabs.f32(float %x)
142139
%r = call nnan reassoc float @cosf(float %fabs)
@@ -147,9 +144,8 @@ define float @cosf_unary_fabs_arg_FMF(float %x) {
147144

148145
define double @cos_copysign_arg(double %x, double %y) {
149146
; ANY-LABEL: @cos_copysign_arg(
150-
; ANY-NEXT: [[COPYSIGN:%.*]] = tail call double @llvm.copysign.f64(double [[X:%.*]], double [[Y:%.*]])
151-
; ANY-NEXT: [[R:%.*]] = call double @cos(double [[COPYSIGN]])
152-
; ANY-NEXT: ret double [[R]]
147+
; ANY-NEXT: [[COS:%.*]] = call double @cos(double [[X:%.*]])
148+
; ANY-NEXT: ret double [[COS]]
153149
;
154150
%copysign = tail call double @llvm.copysign(double %x, double %y)
155151
%r = call double @cos(double %copysign)
@@ -159,9 +155,8 @@ define double @cos_copysign_arg(double %x, double %y) {
159155

160156
define float @cosf_unary_copysign_arg(float %x) {
161157
; ANY-LABEL: @cosf_unary_copysign_arg(
162-
; ANY-NEXT: [[COPYSIGN:%.*]] = call float @llvm.fabs.f32(float [[X:%.*]])
163-
; ANY-NEXT: [[R:%.*]] = call float @cosf(float [[COPYSIGN]])
164-
; ANY-NEXT: ret float [[R]]
158+
; ANY-NEXT: [[COS:%.*]] = call float @cosf(float [[X:%.*]])
159+
; ANY-NEXT: ret float [[COS]]
165160
;
166161
%copysign = tail call float @llvm.copysign.f32(float %x, float 1.0)
167162
%r = call float @cosf(float %copysign)
@@ -170,9 +165,8 @@ define float @cosf_unary_copysign_arg(float %x) {
170165

171166
define float @cosf_copysign_arg_FMF(float %x, float %y) {
172167
; ANY-LABEL: @cosf_copysign_arg_FMF(
173-
; ANY-NEXT: [[COPYSIGN:%.*]] = tail call float @llvm.copysign.f32(float [[X:%.*]], float [[Y:%.*]])
174-
; ANY-NEXT: [[R:%.*]] = call reassoc nnan float @cosf(float [[COPYSIGN]])
175-
; ANY-NEXT: ret float [[R]]
168+
; ANY-NEXT: [[COS:%.*]] = call reassoc nnan float @cosf(float [[X:%.*]])
169+
; ANY-NEXT: ret float [[COS]]
176170
;
177171
%copysign = tail call float @llvm.copysign.f32(float %x, float %y)
178172
%r = call nnan reassoc float @cosf(float %copysign)

0 commit comments

Comments
 (0)