Optimize printf -> iprintf if there are no floating point arguments
authorRichard Osborne <richard@xmos.com>
Thu, 3 Mar 2011 13:17:51 +0000 (13:17 +0000)
committerRichard Osborne <richard@xmos.com>
Thu, 3 Mar 2011 13:17:51 +0000 (13:17 +0000)
and iprintf is available on the target. Currently iprintf is only
marked as being available on the XCore.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@126935 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Target/TargetLibraryInfo.h
lib/Target/TargetLibraryInfo.cpp
lib/Transforms/Scalar/SimplifyLibCalls.cpp
test/Transforms/SimplifyLibCalls/iprintf.ll [new file with mode: 0644]

index bdd214b6b743e5803b862582fbedc6fe7a51161e..bc119e11cb792379a3e8fdc62eb8ac5b9beb8028 100644 (file)
@@ -26,6 +26,9 @@ namespace llvm {
       /// void memset_pattern16(void *b, const void *pattern16, size_t len);
       memset_pattern16,
       
+      /// int iprintf(const char *format, ...);
+      iprintf,
+      
       NumLibFuncs
     };
   }
index c8bed18ffabef33a0b23229dff7130c4c771810b..6bf6c0d1a6bce83a4ac2e039a3b4cf32150d3db8 100644 (file)
@@ -30,7 +30,10 @@ static void initialize(TargetLibraryInfo &TLI, const Triple &T) {
   // memset_pattern16 is only available on iOS 3.0 and Mac OS/X 10.5 and later.
   if (T.getOS() != Triple::Darwin || T.getDarwinMajorNumber() < 9)
     TLI.setUnavailable(LibFunc::memset_pattern16);
-  
+
+  // iprintf is only available on XCore.
+  if (T.getArch() != Triple::xcore)
+    TLI.setUnavailable(LibFunc::iprintf);
 }
 
 
index 9f136d4e3077a2821469d5454b2ca7fa6af008a5..c0bef26291af2f7bb903d35d404f939008359d51 100644 (file)
@@ -49,6 +49,7 @@ class LibCallOptimization {
 protected:
   Function *Caller;
   const TargetData *TD;
+  const TargetLibraryInfo *TLI;
   LLVMContext* Context;
 public:
   LibCallOptimization() { }
@@ -62,9 +63,11 @@ public:
   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
     =0;
 
-  Value *OptimizeCall(CallInst *CI, const TargetData *TD, IRBuilder<> &B) {
+  Value *OptimizeCall(CallInst *CI, const TargetData *TD,
+                      const TargetLibraryInfo *TLI, IRBuilder<> &B) {
     Caller = CI->getParent()->getParent();
     this->TD = TD;
+    this->TLI = TLI;
     if (CI->getCalledFunction())
       Context = &CI->getCalledFunction()->getContext();
 
@@ -97,6 +100,15 @@ static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
   }
   return true;
 }
+static bool CallHasFloatingPointArgument(const CallInst *CI) {
+  for (CallInst::const_op_iterator it = CI->op_begin(), e = CI->op_end();
+       it != e; ++it) {
+    if ((*it)->getType()->isFloatingPointTy())
+      return true;
+  }
+  return false;
+}
 
 /// IsOnlyUsedInEqualityComparison - Return true if it is only used in equality
 /// comparisons with With.
@@ -1075,14 +1087,8 @@ struct ToAsciiOpt : public LibCallOptimization {
 // 'printf' Optimizations
 
 struct PrintFOpt : public LibCallOptimization {
-  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
-    // Require one fixed pointer argument and an integer/void result.
-    const FunctionType *FT = Callee->getFunctionType();
-    if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
-        !(FT->getReturnType()->isIntegerTy() ||
-          FT->getReturnType()->isVoidTy()))
-      return 0;
-
+  Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
+                                   IRBuilder<> &B) {
     // Check for a fixed format string.
     std::string FormatStr;
     if (!GetConstantStringInfo(CI->getArgOperand(0), FormatStr))
@@ -1138,6 +1144,32 @@ struct PrintFOpt : public LibCallOptimization {
     }
     return 0;
   }
+
+  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
+    // Require one fixed pointer argument and an integer/void result.
+    const FunctionType *FT = Callee->getFunctionType();
+    if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
+        !(FT->getReturnType()->isIntegerTy() ||
+          FT->getReturnType()->isVoidTy()))
+      return 0;
+
+    if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) {
+      return V;
+    }
+
+    // printf(format, ...) -> iprintf(format, ...) if no floating point
+    // arguments.
+    if (TLI->has(LibFunc::iprintf) && !CallHasFloatingPointArgument(CI)) {
+      Module *M = B.GetInsertBlock()->getParent()->getParent();
+      Constant *IPrintFFn =
+        M->getOrInsertFunction("iprintf", FT, Callee->getAttributes());
+      CallInst *New = cast<CallInst>(CI->clone());
+      New->setCalledFunction(IPrintFFn);
+      B.Insert(New);
+      return New;
+    }
+    return 0;
+  }
 };
 
 //===---------------------------------------===//
@@ -1545,7 +1577,7 @@ bool SimplifyLibCalls::runOnFunction(Function &F) {
       Builder.SetInsertPoint(BB, I);
 
       // Try to optimize this call.
-      Value *Result = LCO->OptimizeCall(CI, TD, Builder);
+      Value *Result = LCO->OptimizeCall(CI, TD, TLI, Builder);
       if (Result == 0) continue;
 
       DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
diff --git a/test/Transforms/SimplifyLibCalls/iprintf.ll b/test/Transforms/SimplifyLibCalls/iprintf.ll
new file mode 100644 (file)
index 0000000..11385cb
--- /dev/null
@@ -0,0 +1,29 @@
+; RUN: opt < %s -simplify-libcalls -S -o %t
+; RUN: FileCheck < %t %s
+target datalayout = "e-p:32:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-i64:32:32-f32:32:32-f64:32:32-v64:64:64-v128:128:128-a0:0:32"
+target triple = "xcore-xmos-elf"
+
+@.str = internal constant [4 x i8] c"%f\0A\00"         ; <[4 x i8]*> [#uses=1]
+@.str1 = internal constant [4 x i8] c"%d\0A\00"                ; <[4 x i8]*> [#uses=1]
+
+; Verify printf with no floating point arguments is transformed to iprintf
+define i32 @f0(i32 %x) nounwind {
+entry:
+; CHECK: define i32 @f0
+; CHECK: @iprintf
+; CHECK: }
+       %0 = tail call i32 (i8*, ...)* @printf(i8* getelementptr ([4 x i8]* @.str1, i32 0, i32 0), i32 %x)              ; <i32> [#uses=0]
+       ret i32 %0
+}
+
+; Verify we don't turn this into an iprintf call
+define void @f1(double %x) nounwind {
+entry:
+; CHECK: define void @f1
+; CHECK: @printf
+; CHECK: }
+       %0 = tail call i32 (i8*, ...)* @printf(i8* getelementptr ([4 x i8]* @.str, i32 0, i32 0), double %x) nounwind           ; <i32> [#uses=0]
+       ret void
+}
+
+declare i32 @printf(i8* nocapture, ...) nounwind