summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms/Utils
diff options
context:
space:
mode:
authorSanjay Patel <spatel@rotateright.com>2018-08-13 19:24:41 +0000
committerSanjay Patel <spatel@rotateright.com>2018-08-13 19:24:41 +0000
commite45a83d4479434d7bd6d7a3adc9e88bc159f2eb1 (patch)
treea732af733941d2dc432a00b835d0d66e7e70565a /llvm/lib/Transforms/Utils
parentf138fda5edc21004de744ed354e9142b8c2ee109 (diff)
downloadbcm5719-llvm-e45a83d4479434d7bd6d7a3adc9e88bc159f2eb1.tar.gz
bcm5719-llvm-e45a83d4479434d7bd6d7a3adc9e88bc159f2eb1.zip
[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
Diffstat (limited to 'llvm/lib/Transforms/Utils')
-rw-r--r--llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp42
1 files changed, 27 insertions, 15 deletions
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 6636f71a6d4..eda3f059e69 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -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:
OpenPOWER on IntegriCloud