SimplifyLibCalls: Replace more unary libcalls with intrinsics

llvm-svn: 292855
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 3ab933d..4ea051e 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -948,6 +948,20 @@
   return B.CreateFPExt(V, B.getDoubleTy());
 }
 
+// Replace a libcall \p CI with a call to intrinsic \p IID
+static Value *replaceUnaryCall(CallInst *CI, IRBuilder<> &B, Intrinsic::ID IID) {
+  // Propagate fast-math flags from the existing call to the new call.
+  IRBuilder<>::FastMathFlagGuard Guard(B);
+  B.setFastMathFlags(CI->getFastMathFlags());
+
+  Module *M = CI->getModule();
+  Value *V = CI->getArgOperand(0);
+  Function *F = Intrinsic::getDeclaration(M, IID, CI->getType());
+  CallInst *NewCall = B.CreateCall(F, V);
+  NewCall->takeName(CI);
+  return NewCall;
+}
+
 /// Shrink double -> float for binary functions like 'fmin/fmax'.
 static Value *optimizeBinaryDoubleFP(CallInst *CI, IRBuilder<> &B) {
   Function *Callee = CI->getCalledFunction();
@@ -1210,19 +1224,6 @@
   return Ret;
 }
 
-Value *LibCallSimplifier::optimizeFabs(CallInst *CI, IRBuilder<> &B) {
-  Function *Callee = CI->getCalledFunction();
-  IRBuilder<>::FastMathFlagGuard Guard(B);
-  B.setFastMathFlags(CI->getFastMathFlags());
-
-  // fabs/fabsf -> llvm.fabs.*
-  Value *F = Intrinsic::getDeclaration(Callee->getParent(), Intrinsic::fabs,
-                                       CI->getType());
-  Value *NewCall = B.CreateCall(F, { CI->getArgOperand(0) });
-  NewCall->takeName(CI);
-  return NewCall;
-}
-
 Value *LibCallSimplifier::optimizeFMinFMax(CallInst *CI, IRBuilder<> &B) {
   Function *Callee = CI->getCalledFunction();
   // If we can shrink the call to a float function rather than a double
@@ -2091,7 +2092,7 @@
     case LibFunc_fabsf:
     case LibFunc_fabs:
     case LibFunc_fabsl:
-      return optimizeFabs(CI, Builder);
+      return replaceUnaryCall(CI, Builder, Intrinsic::fabs);
     case LibFunc_sqrtf:
     case LibFunc_sqrt:
     case LibFunc_sqrtl:
@@ -2144,14 +2145,16 @@
     case LibFunc_fputc:
       return optimizeErrorReporting(CI, Builder, 1);
     case LibFunc_ceil:
+      return replaceUnaryCall(CI, Builder, Intrinsic::ceil);
     case LibFunc_floor:
-    case LibFunc_rint:
+      return replaceUnaryCall(CI, Builder, Intrinsic::floor);
     case LibFunc_round:
+      return replaceUnaryCall(CI, Builder, Intrinsic::round);
     case LibFunc_nearbyint:
+    case LibFunc_rint:
+      return replaceUnaryCall(CI, Builder, Intrinsic::nearbyint);
     case LibFunc_trunc:
-      if (hasFloatVersion(FuncName))
-        return optimizeUnaryDoubleFP(CI, Builder, false);
-      return nullptr;
+      return replaceUnaryCall(CI, Builder, Intrinsic::trunc);
     case LibFunc_acos:
     case LibFunc_acosh:
     case LibFunc_asin:
@@ -2215,16 +2218,10 @@
 //   * log(exp10(y)) -> y*log(10)
 //   * log(sqrt(x))  -> 0.5*log(x)
 //
-// lround, lroundf, lroundl:
-//   * lround(cnst) -> cnst'
-//
 // pow, powf, powl:
 //   * pow(sqrt(x),y) -> pow(x,y*0.5)
 //   * pow(pow(x,y),z)-> pow(x,y*z)
 //
-// round, roundf, roundl:
-//   * round(cnst) -> cnst'
-//
 // signbit:
 //   * signbit(cnst) -> cnst'
 //   * signbit(nncst) -> 0 (if pstv is a non-negative constant)
@@ -2234,10 +2231,6 @@
 //   * sqrt(Nroot(x)) -> pow(x,1/(2*N))
 //   * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
 //
-// trunc, truncf, truncl:
-//   * trunc(cnst) -> cnst'
-//
-//
 
 //===----------------------------------------------------------------------===//
 // Fortified Library Call Optimizations