[llvm-commits] [PATCH] Optimize printf -> iprintf if there are no floating point arguments

Chris Lattner clattner at apple.com
Tue Mar 1 17:15:28 PST 2011


On Mar 1, 2011, at 9:05 AM, Richard Osborne wrote:

> This patch adds an optimization to SimplifyLibCalls which transforms calls to printf into iprintf calls. iprintf is a restricted version of printf provided by newlib without floating-point formatting (see http://lists.cs.uiuc.edu/pipermail/llvmdev/2011-February/037832.html).
> 
> This patch uses TargetLibraryInfo to determine whether iprintf is available. Currently it is only marked as available on the XCore target.
> 
> I intend to add similar optimizations for sprintf / fprintf once this patch is accepted. Please let me know if this is OK to commit.

In addition to Frits' comment about the testcase, the patch would be simpler if you changed this:

+    // printf(format, ...) -> iprintf(format, ...) if no floating point
+    // arguments.
+    if (TLI->has(LibFunc::iprintf) && !CallHasFloatingPointArgument(CI)) {
+      return EmitIPrintF(CI->op_begin(), CI->op_end(), B, TD);
+    }

Instead of emitting a new IPrintF call, you can just change the callee of the existing call to be iprintf instead of printf.  This would make the code look something like this:

    // printf(format, ...) -> iprintf(format, ...) if no floating point
    // arguments.
    if (TLI->has(LibFunc::iprintf) && !CallHasFloatingPointArgument(CI)) {
      Constant *IPrintFFn = ...
      CI->setOperand(0, IPrintFFn);
      return CI;
    }

This allows you to remove the EmitIPrintF implementation and should simplify the fprintf case that's coming next.

Thanks for implementing this Richard!

-Chris



More information about the llvm-commits mailing list