1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-20 03:23:01 +02:00

[SimplifyLibCalls] add reflection fold for -sin(-x) (PR38458)

This is a very partial fix for the reported problem. I suspect
we do not get this fold in most motivating cases because most of
the time, the libcall would have been replaced by an intrinsic,
and that optimization is handled elsewhere...but maybe it should
be handled here?

llvm-svn: 339604
This commit is contained in:
Sanjay Patel 2018-08-13 19:24:41 +00:00
parent 84a5e162d3
commit e888d5c838
3 changed files with 35 additions and 26 deletions

View File

@ -131,7 +131,6 @@ private:
// Math Library Optimizations
Value *optimizeCAbs(CallInst *CI, IRBuilder<> &B);
Value *optimizeCos(CallInst *CI, IRBuilder<> &B);
Value *optimizePow(CallInst *CI, IRBuilder<> &B);
Value *replacePowWithSqrt(CallInst *Pow, IRBuilder<> &B);
Value *optimizeExp2(CallInst *CI, IRBuilder<> &B);

View File

@ -1122,18 +1122,30 @@ Value *LibCallSimplifier::optimizeCAbs(CallInst *CI, IRBuilder<> &B) {
return B.CreateCall(FSqrt, B.CreateFAdd(RealReal, ImagImag), "cabs");
}
Value *LibCallSimplifier::optimizeCos(CallInst *CI, IRBuilder<> &B) {
Function *Callee = CI->getCalledFunction();
StringRef Name = Callee->getName();
if (UnsafeFPShrink && Name == "cos" && hasFloatVersion(Name))
if (Value *V = optimizeUnaryDoubleFP(CI, B, true))
return V;
// cos(-X) -> cos(X)
static Value *optimizeTrigReflections(CallInst *Call, LibFunc Func,
IRBuilder<> &B) {
// FIXME: This drops FMF.
// TODO: Add tan() and other calls.
// TODO: Can this be shared to also handle LLVM intrinsics?
Value *X;
if (match(CI->getArgOperand(0), m_FNeg(m_Value(X))))
return B.CreateCall(Callee, X, "cos");
switch (Func) {
case LibFunc_sin:
case LibFunc_sinf:
case LibFunc_sinl:
// sin(-X) --> -sin(X)
if (match(Call->getArgOperand(0), m_OneUse(m_FNeg(m_Value(X)))))
return B.CreateFNeg(B.CreateCall(Call->getCalledFunction(), X, "sin"));
break;
case LibFunc_cos:
case LibFunc_cosf:
case LibFunc_cosl:
// cos(-X) --> cos(X)
if (match(Call->getArgOperand(0), m_FNeg(m_Value(X))))
return B.CreateCall(Call->getCalledFunction(), X, "cos");
break;
default:
break;
}
return nullptr;
}
@ -2327,11 +2339,10 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
if (CI->isStrictFP())
return nullptr;
if (Value *V = optimizeTrigReflections(CI, Func, Builder))
return V;
switch (Func) {
case LibFunc_cosf:
case LibFunc_cos:
case LibFunc_cosl:
return optimizeCos(CI, Builder);
case LibFunc_sinpif:
case LibFunc_sinpi:
case LibFunc_cospif:
@ -2386,6 +2397,7 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
case LibFunc_exp:
case LibFunc_exp10:
case LibFunc_expm1:
case LibFunc_cos:
case LibFunc_sin:
case LibFunc_sinh:
case LibFunc_tanh:

View File

@ -52,9 +52,9 @@ define float @cosf_negated_arg_FMF(float %x) {
define double @sin_negated_arg(double %x) {
; ANY-LABEL: @sin_negated_arg(
; ANY-NEXT: [[NEG:%.*]] = fsub double -0.000000e+00, [[X:%.*]]
; ANY-NEXT: [[R:%.*]] = call double @sin(double [[NEG]])
; ANY-NEXT: ret double [[R]]
; ANY-NEXT: [[SIN:%.*]] = call double @sin(double [[X:%.*]])
; ANY-NEXT: [[TMP1:%.*]] = fsub double -0.000000e+00, [[SIN]]
; ANY-NEXT: ret double [[TMP1]]
;
%neg = fsub double -0.0, %x
%r = call double @sin(double %neg)
@ -63,9 +63,9 @@ define double @sin_negated_arg(double %x) {
define float @sinf_negated_arg(float %x) {
; ANY-LABEL: @sinf_negated_arg(
; ANY-NEXT: [[NEG:%.*]] = fsub float -0.000000e+00, [[X:%.*]]
; ANY-NEXT: [[R:%.*]] = call float @sinf(float [[NEG]])
; ANY-NEXT: ret float [[R]]
; ANY-NEXT: [[SIN:%.*]] = call float @sinf(float [[X:%.*]])
; ANY-NEXT: [[TMP1:%.*]] = fsub float -0.000000e+00, [[SIN]]
; ANY-NEXT: ret float [[TMP1]]
;
%neg = fsub float -0.0, %x
%r = call float @sinf(float %neg)
@ -92,10 +92,8 @@ define double @sin_negated_arg_extra_use(double %x) {
define double @neg_sin_negated_arg(double %x) {
; ANY-LABEL: @neg_sin_negated_arg(
; ANY-NEXT: [[NEG:%.*]] = fsub double -0.000000e+00, [[X:%.*]]
; ANY-NEXT: [[R:%.*]] = call double @sin(double [[NEG]])
; ANY-NEXT: [[RN:%.*]] = fsub double -0.000000e+00, [[R]]
; ANY-NEXT: ret double [[RN]]
; ANY-NEXT: [[SIN:%.*]] = call double @sin(double [[X:%.*]])
; ANY-NEXT: ret double [[SIN]]
;
%neg = fsub double -0.0, %x
%r = call double @sin(double %neg)