[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
This commit is contained in:
Sanjay Patel 2018-08-16 22:46:20 +00:00
parent 7f9b4af184
commit 8ba631d9c8
2 changed files with 11 additions and 8 deletions

View File

@ -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:

View File

@ -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)