[PATCH] D31533: Do not translate rint into nearbyint, but truncate it like nearbyint

Joerg Sonnenberger via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 31 06:52:47 PDT 2017


joerg created this revision.
Herald added a subscriber: wdng.

A common way to implement nearbyint is by fiddling with the floating point environment and calling rint. This is used at least by the BSD libm and musl. As such, canonicalizing the latter to the former will create infinite loops for libm and generally pessimize performance, at least when the generic C versions are used. This patch preserves the rint in the libcall translation and also handle the domain truncation logic, so that rint with float argument will be reduced to rintf etc.


Repository:
  rL LLVM

https://reviews.llvm.org/D31533

Files:
  lib/Transforms/InstCombine/InstCombineCalls.cpp
  lib/Transforms/Utils/SimplifyLibCalls.cpp
  test/Transforms/InstCombine/float-shrink-compare.ll


Index: test/Transforms/InstCombine/float-shrink-compare.ll
===================================================================
--- test/Transforms/InstCombine/float-shrink-compare.ll
+++ test/Transforms/InstCombine/float-shrink-compare.ll
@@ -119,7 +119,7 @@
   %cmp.ext = zext i1 %cmp to i32
   ret i32 %cmp.ext
 ; CHECK-LABEL: @test5(
-; CHECK-NEXT: %rint = call float @llvm.nearbyint.f32(float %x)
+; CHECK-NEXT: %rint = call float @llvm.rint.f32(float %x)
 ; CHECK-NEXT: fcmp oeq float %rint, %y
 }
 
@@ -276,7 +276,7 @@
   %cmp.ext = zext i1 %cmp to i32
   ret i32 %cmp.ext
 ; CHECK-LABEL: @test12(
-; CHECK-NEXT: %rint = call float @llvm.nearbyint.f32(float %x)
+; CHECK-NEXT: %rint = call float @llvm.rint.f32(float %x)
 ; CHECK-NEXT: fcmp oeq float %rint, %y
 }
 
Index: lib/Transforms/Utils/SimplifyLibCalls.cpp
===================================================================
--- lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -2160,8 +2160,9 @@
     case LibFunc_round:
       return replaceUnaryCall(CI, Builder, Intrinsic::round);
     case LibFunc_nearbyint:
-    case LibFunc_rint:
       return replaceUnaryCall(CI, Builder, Intrinsic::nearbyint);
+    case LibFunc_rint:
+      return replaceUnaryCall(CI, Builder, Intrinsic::rint);
     case LibFunc_trunc:
       return replaceUnaryCall(CI, Builder, Intrinsic::trunc);
     case LibFunc_acos:
Index: lib/Transforms/InstCombine/InstCombineCalls.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -2091,6 +2091,7 @@
   case Intrinsic::floor:
   case Intrinsic::round:
   case Intrinsic::nearbyint:
+  case Intrinsic::rint:
   case Intrinsic::trunc: {
     Value *ExtSrc;
     if (match(II->getArgOperand(0), m_FPExt(m_Value(ExtSrc))) &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D31533.93632.patch
Type: text/x-patch
Size: 1879 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170331/b8b9607d/attachment.bin>


More information about the llvm-commits mailing list