From 8ba631d9c8b5f1b4e4e314c1f4b366c58a71b4a5 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Thu, 16 Aug 2018 22:46:20 +0000 Subject: [PATCH] [InstCombine] add reflection fold for tan(-x) This is a follow-up suggested with rL339604. For tan(), we don't have a corresponding LLVM intrinsic -- unlike sin/cos -- so this is the only way/place that we can do this fold currently. llvm-svn: 339958 --- llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp | 7 +++++-- llvm/test/Transforms/InstCombine/cos-1.ll | 12 ++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp index a0f1f8185ece..865720b416b9 100644 --- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -1130,16 +1130,19 @@ static Value *optimizeTrigReflections(CallInst *Call, LibFunc Func, IRBuilder<>::FastMathFlagGuard Guard(B); B.setFastMathFlags(Call->getFastMathFlags()); - // TODO: Add tan() and other calls. // TODO: Can this be shared to also handle LLVM intrinsics? Value *X; switch (Func) { case LibFunc_sin: case LibFunc_sinf: case LibFunc_sinl: + case LibFunc_tan: + case LibFunc_tanf: + case LibFunc_tanl: // sin(-X) --> -sin(X) + // tan(-X) --> -tan(X) if (match(Call->getArgOperand(0), m_OneUse(m_FNeg(m_Value(X))))) - return B.CreateFNeg(B.CreateCall(Call->getCalledFunction(), X, "sin")); + return B.CreateFNeg(B.CreateCall(Call->getCalledFunction(), X)); break; case LibFunc_cos: case LibFunc_cosf: diff --git a/llvm/test/Transforms/InstCombine/cos-1.ll b/llvm/test/Transforms/InstCombine/cos-1.ll index 9900f191bfbb..50db2a98e83b 100644 --- a/llvm/test/Transforms/InstCombine/cos-1.ll +++ b/llvm/test/Transforms/InstCombine/cos-1.ll @@ -117,9 +117,9 @@ define double @neg_sin_negated_arg(double %x) { define double @tan_negated_arg(double %x) { ; ANY-LABEL: @tan_negated_arg( -; ANY-NEXT: [[NEG:%.*]] = fsub double -0.000000e+00, [[X:%.*]] -; ANY-NEXT: [[R:%.*]] = call double @tan(double [[NEG]]) -; ANY-NEXT: ret double [[R]] +; ANY-NEXT: [[TMP1:%.*]] = call double @tan(double [[X:%.*]]) +; ANY-NEXT: [[TMP2:%.*]] = fsub double -0.000000e+00, [[TMP1]] +; ANY-NEXT: ret double [[TMP2]] ; %neg = fsub double -0.0, %x %r = call double @tan(double %neg) @@ -130,9 +130,9 @@ define double @tan_negated_arg(double %x) { define fp128 @tanl_negated_arg(fp128 %x) { ; ANY-LABEL: @tanl_negated_arg( -; ANY-NEXT: [[NEG:%.*]] = fsub fp128 0xL00000000000000008000000000000000, [[X:%.*]] -; ANY-NEXT: [[R:%.*]] = call fp128 @tanl(fp128 [[NEG]]) -; ANY-NEXT: ret fp128 [[R]] +; ANY-NEXT: [[TMP1:%.*]] = call fp128 @tanl(fp128 [[X:%.*]]) +; ANY-NEXT: [[TMP2:%.*]] = fsub fp128 0xL00000000000000008000000000000000, [[TMP1]] +; ANY-NEXT: ret fp128 [[TMP2]] ; %neg = fsub fp128 0xL00000000000000008000000000000000, %x %r = call fp128 @tanl(fp128 %neg)