[PATCH] D28110: [InstCombiner] Simplify lib calls to `round{,f}`

bryant via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 25 14:30:43 PST 2016


bryant created this revision.
bryant added reviewers: spatel, RKSimon, majnemer.
bryant added a subscriber: llvm-commits.
bryant set the repository for this revision to rL LLVM.

From IRC:

  12:30:04 -!- Noxbru [~Noxbru at 170.red-2-139-2.dynamicip.rima-tde.net] has joined #llvm
  12:32:20 < Noxbru> hi, I have a quick question about llvm: is there a way to make the optimizer optimize away the calls to 'round' when the arguments are known constants?
  12:33:09 < Noxbru> for example, I have 'round(0.9)' and GCC -O2 simply puts a '1' while clang makes the full call to round
  12:33:24 < Noxbru> at least when checking the assembly output
  12:36:36 < Noxbru> BTW, this is with clang 3.9.0
  12:38:10 < gronsaken> gamester: See the answer from Duncan in this thread https://groups.google.com/forum/#!topic/llvm-dev/uk4uiK93jeM
  12:46:48 < davide> Noxbru: missing optimization in SimplifyLibCalls.cpp
  12:46:58 < davide> Noxbru: open a bug
  12:48:16 < Noxbru> davide: ah, okay, against which component should I file it?
  12:48:33 < davide> scalar optimizations
  12:48:39 < davide> libraries
  12:48:49 < davide> it should be easy to fix, FWIW


Repository:
  rL LLVM

https://reviews.llvm.org/D28110

Files:
  lib/Analysis/ConstantFolding.cpp
  test/Transforms/ConstProp/calls.ll


Index: test/Transforms/ConstProp/calls.ll
===================================================================
--- test/Transforms/ConstProp/calls.ll
+++ test/Transforms/ConstProp/calls.ll
@@ -16,6 +16,7 @@
 declare double @log(double) readnone nounwind
 declare double @log10(double) readnone nounwind
 declare double @pow(double, double) readnone nounwind
+declare double @round(double) readnone nounwind
 declare double @sin(double) readnone nounwind
 declare double @sinh(double) readnone nounwind
 declare double @sqrt(double) readnone nounwind
@@ -37,6 +38,7 @@
 declare float @logf(float) readnone nounwind
 declare float @log10f(float) readnone nounwind
 declare float @powf(float, float) readnone nounwind
+declare float @roundf(float) readnone nounwind
 declare float @sinf(float) readnone nounwind
 declare float @sinhf(float) readnone nounwind
 declare float @sqrtf(float) readnone nounwind
@@ -102,6 +104,9 @@
   %14 = call double @pow(double 3.000000e+00, double 4.000000e+00)
   store double %14, double* %slot
 ; FNOBUILTIN: call
+  %round_val = call double @round(double 3.000000e+00)
+  store double %round_val, double* %slot
+; FNOBUILTIN: call
   %15 = call double @sinh(double 3.000000e+00)
   store double %15, double* %slot
 ; FNOBUILTIN: call
@@ -153,6 +158,9 @@
   %31 = call float @powf(float 3.000000e+00, float 4.000000e+00)
   store float %31, float* %slotf
 ; FNOBUILTIN: call
+  %roundf_val = call float @roundf(float 3.000000e+00)
+  store float %roundf_val, float* %slotf
+; FNOBUILTIN: call
   %32 = call float @sinf(float 3.000000e+00)
   store float %32, float* %slotf
 ; FNOBUILTIN: call
Index: lib/Analysis/ConstantFolding.cpp
===================================================================
--- lib/Analysis/ConstantFolding.cpp
+++ lib/Analysis/ConstantFolding.cpp
@@ -1431,6 +1431,8 @@
            Name == "log10f";
   case 'p':
     return Name == "pow" || Name == "powf";
+  case 'r':
+    return Name == "round" || Name == "roundf";
   case 's':
     return Name == "sin" || Name == "sinh" || Name == "sqrt" ||
            Name == "sinf" || Name == "sinhf" || Name == "sqrtf";
@@ -1695,6 +1697,10 @@
           }
         }
         break;
+      case 'r':
+        if ((Name == "round" && TLI->has(LibFunc::round)) ||
+            (Name == "roundf" && TLI->has(LibFunc::roundf)))
+          return ConstantFoldFP(round, V, Ty);
       case 's':
         if ((Name == "sin" && TLI->has(LibFunc::sin)) ||
             (Name == "sinf" && TLI->has(LibFunc::sinf)))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D28110.82480.patch
Type: text/x-patch
Size: 2507 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161225/4af50c9a/attachment.bin>


More information about the llvm-commits mailing list