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

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 13 12:24:41 PDT 2018


Author: spatel
Date: Mon Aug 13 12:24:41 2018
New Revision: 339604

URL: http://llvm.org/viewvc/llvm-project?rev=339604&view=rev
Log:
[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?

Modified:
    llvm/trunk/include/llvm/Transforms/Utils/SimplifyLibCalls.h
    llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
    llvm/trunk/test/Transforms/InstCombine/cos-1.ll

Modified: llvm/trunk/include/llvm/Transforms/Utils/SimplifyLibCalls.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Utils/SimplifyLibCalls.h?rev=339604&r1=339603&r2=339604&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Transforms/Utils/SimplifyLibCalls.h (original)
+++ llvm/trunk/include/llvm/Transforms/Utils/SimplifyLibCalls.h Mon Aug 13 12:24:41 2018
@@ -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);

Modified: llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp?rev=339604&r1=339603&r2=339604&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp Mon Aug 13 12:24:41 2018
@@ -1122,18 +1122,30 @@ Value *LibCallSimplifier::optimizeCAbs(C
   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::optimizeFloati
   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::optimizeFloati
   case LibFunc_exp:
   case LibFunc_exp10:
   case LibFunc_expm1:
+  case LibFunc_cos:
   case LibFunc_sin:
   case LibFunc_sinh:
   case LibFunc_tanh:

Modified: llvm/trunk/test/Transforms/InstCombine/cos-1.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/cos-1.ll?rev=339604&r1=339603&r2=339604&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/cos-1.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/cos-1.ll Mon Aug 13 12:24:41 2018
@@ -52,9 +52,9 @@ define float @cosf_negated_arg_FMF(float
 
 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
 
 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)




More information about the llvm-commits mailing list